http://wenku.baidu.com/view/77a2181e10a6f524ccbf854b.html dom4j_API
http://wenku.baidu.com/view/7b1b3f79168884868762d607.html
http://www.cnblogs.com/shitianzeng/articles/2518323.html
http://www.cnblogs.com/shitianzeng/articles/2518323.html
1. Document object related
1. Read the XML file and get the document object.
SAXReader reader = new SAXReader();
Document document = reader.read(new File("input.xml"));
2. Parse the text in XML form and get the document object.
String text = "<members></members>";
Document document = DocumentHelper.parseText(text);
3. Actively create the document object.
Document document = DocumentHelper.createDocument();
Element root = document.addElement("members");//Create a root node
2. Node related
1. Get the root node of the document.
Element rootElm = document.getRootElement();
2. Get a single child node of a node.
Element memberElm=root.element("member");// "member" is the node name
3. Get the text of the node
String text=memberElm.getText();
You can also use:
String text=root.elementText("name"); This is the text to get the name byte point under the root node.
4. Get all the byte points named "member" under a node and traverse it.
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
5. Traverse all child nodes under a node.
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
// do something
}
6. Add child nodes under a node.
Element ageElm = newMemberElm.addElement("age");
7. Set the node text.
ageElm.setText("29");
8. Delete a node.
parentElm.remove(childElm);// childElm is the node to be deleted, parentElm is its parent node
3. Attribute related.
1. Get an attribute under a node
Element root=document.getRootElement();
Attribute attribute=root.attribute("size");// 属性名name
2. Get the text of the attribute
String text=attribute.getText();
You can also use:
String text2=root.element("name").attributeValue("firstname"); This is to get the value of the attribute firstname of the name byte point under the root node.
3. Traverse all attributes of a node
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
4. Set the attributes and text of a node.
newMemberElm.addAttribute("name", "sitinspring");
5. Set the text of the attribute
Attribute attribute=root.attribute("name");
attribute.setText("sitinspring");
6. Delete an attribute
Attribute attribute=root.attribute("size");// 属性名name
root.remove(attribute);
4. Write the document to the XML file.
1. The document is all in English, no encoding is set, and it is written directly.
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(document);
writer.close();
2. The document contains Chinese, set the encoding format to write.
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK"); // Specify XML encoding
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"),format);
writer.write(document);
writer.close();
Five. String and XML conversion
1. Convert the string to XML
String text = "<members> <member>sitinspring</member> </members>";
Document document = DocumentHelper.parseText(text);
2. Convert the XML of the document or node to a string.
SAXReader reader = new SAXReader();
Document document = reader.read(new File("input.xml"));
Element root=document.getRootElement();
String docXmlText=document.asXML();
String rootXmlText=root.asXML();
Element memberElm=root.element("member");
String memberXmlText=memberElm.asXML();
6. Use XPath to quickly find nodes.
Example of an XML document read
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MemberManagement</name>
<comment></comment>
<projects>
<project>PRJ1</project>
<project>PRJ2</project>
<project>PRJ3</project>
<project>PRJ4</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Quickly find node projects using XPath.
public static void main(String[] args){
SAXReader reader = new SAXReader();
try{
Document doc = reader.read(new File("sample.xml"));
List projects=doc.selectNodes("/projectDescription/projects/project");
Iterator it=projects.iterator();
while(it.hasNext()){
Element elm=(Element)it.next();
System.out.println(elm.getText());
}
}
catch(Exception ex){
ex.printStackTrace();
}
example:
package com.stz;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
import java.util.List;
public class TestDate {
public static void main(String[]args){
String xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<students>"+
" <student id=\"1\">"+
" <name>stz</name>"+
" <age>23</age>"+
"</student>"+
"<student id=\"2\">"+
"<name>rhl</name>"+
"<age>23</age>"+
"</student>"+
"<student id=\"3\">"+
" <name>tom</name>"+
" <age>23</age>"+
"</student>"+
"</students>";
try {
Document document=DocumentHelper.parseText(xml);
Element root=document.getRootElement();
//System.out.println(ele.e);
//List nodes=root.elements("student");
//System.out.println(root.getName());
//root.elementIterator();
List nodes=document.selectNodes("/students/student");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
System.out.println("id:"+elm.attributeValue("id"));
System.out.println(elm.getName());
Element eles=elm.element("name");
System.out.println(eles.getText());
Element elea=elm.element("age");
System.out.println(elea.getText());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
}