To use dom4j to read and write XML documents, you need to first download the dom4j package, dom4j official website in http://www.dom4j.org/
There are two packages after the UNDO, only the XML document to operate the dom4j-1.6.1.jar to add the project can be, if you need to use XPath also need to add the package jaxen-1.1-beta-7.jar.
Perform the following operations:
I. Document Object-related
1. Read the XML file and obtain the document object.
Saxreader reader = new saxreader ();
Document document = reader. Read (new file ("input. xml "));
2. parse the text in XML format to obtain the document object.
String text = "<members> </members> ";
Document document = incluenthelper. parsetext (text );
3. Create a Document Object.
Document document = incluenthelper. createdocument ();
Element root = Document. addelement ("members"); // create a root node
Ii. node-related
1. Get the root node of the document.
Element rootelm = Document. getrootelement ();
2. Obtain a single subnode of a node.
Element memberelm = root. element ("member"); // "member" is the node name.
3. Get the node text
String text = memberelm. gettext ();
You can also use:
String text = root. elementtext ("name"); this is the text that retrieves the name byte under the root node.
4. retrieve all the byte points under a node named "member" 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 subnodes under a node.
For (iterator it = root. elementiterator (); it. hasnext ();){
Element element = (element) it. Next ();
// Do something
}
6. Add a subnode 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, and parentelm is the parent node.
3. property-related.
1. Obtain an attribute under a node
Element root = Document. getrootelement ();
Attribute attribute = root. Attribute ("size"); // attribute name
2. Get the attribute text
String text = attribute. gettext ();
You can also use:
String text2 = root. element ("name"). attributevalue ("firstname"); this is the value of the firstname attribute 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 attributes and text of a node.
Newmemberelm. addattrielm ("name", "sitinspring ");
5. Set the attribute text
Attribute attribute = root. Attribute ("name ");
Attribute. settext ("sitinspring ");
6. delete an attribute
Attribute attribute = root. Attribute ("size"); // attribute name
Root. Remove (attribute );
4. Write documents into XML files.
1. The document is in full English and is written directly without encoding.
Xmlwriter writer = new xmlwriter (New filewriter ("output. xml "));
Writer. Write (document );
Writer. Close ();
2. The document contains Chinese characters and sets the encoding format for writing.
Outputformat format = outputformat. createprettyprint ();
Format. setencoding ("GBK"); // specify the XML Encoding
Xmlwriter writer = new xmlwriter (New filewriter ("output. xml"), format );
Writer. Write (document );
Writer. Close ();
5. Conversion of strings and XML
1. convert a string to XML
String text = "<members> <member> sitinspring </member> </members> ";
Document document = incluenthelper. parsetext (text );
2. Convert the XML of the document or node into 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 a node.
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>
Use XPath to quickly find the 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 ();
}
}