To read and write XML documents using DOM4J, you need to download the DOM4J package first, dom4j the official website in http://www.dom4j.org/
Currently the latest dom4j package download address: Http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip
After unlocking there are two packages, just manipulate the XML document to add Dom4j-1.6.1.jar to the project, if you need to use XPath, you also need to add the package Jaxen-1.1-beta-7.jar.
The following are the relevant actions:
I. 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 to get the Document object.
String Text = "<members></members>";
Document document = Documenthelper.parsetext (text);
3. Proactively create document objects.
Document document = Documenthelper.createdocument ();
Element root = Document.addelement ("members");//Create root node
Two. Node-related
1. Gets the root node of the document.
Element Rootelm = Document.getrootelement ();
2. Obtain a single child node of a node.
Element memberelm=root.element ("member");//"member" is a node name
3. Get the text of the node
String Text=memberelm.gettext ();
You can also use:
String text=root.elementtext ("name"), which is the text that gets the name byte point under the root node.
4. Obtain all the byte points named "member" under a node and traverse them.
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 a child node 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
Three. Attribute-related.
1. Get a property under a node
Element root=document.getrootelement ();
Attribute attribute=root.attribute ("size");//Property name 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 the value of the property firstname that gets the name byte point under the root node.
3. Traverse all properties 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 properties and text of a node.
Newmemberelm.addattribute ("name", "sitinspring");
5. Set the text of a property
Attribute Attribute=root.attribute ("name");
Attribute.settext ("sitinspring");
6. Delete a property
Attribute attribute=root.attribute ("size");//Property name Name
Root.remove (attribute);
Four. Write the document to an XML file.
1. The document is all in English, does not set the encoding, the direct writing form.
XMLWriter writer = new XMLWriter (New FileWriter ("Output.xml"));
Writer.write (document);
Writer.close ();
2. The document contains Chinese, formatting the format of the written form of the encoding.
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. Conversion of strings to XML
1. Converting a string to XML
String Text = "<members> <member>sitinspring</member> </members>";
Document document = Documenthelper.parsetext (text);
2. Convert the XML of a 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 ();
Six. Use XPath to quickly locate the node.
Example of a read XML document
<?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>
Use XPath to quickly find node project.
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 ();
}
}