Dom4j and dom4j Parse xml

Source: Internet
Author: User
Tags xml cdata ibm developerworks

Dom4j and dom4j Parse xml

What is Dom4j DD?

Dom4j is a Java xml api, similar to jdom, used to read and write XML files. Dom4j is an excellent Java xml api with excellent performance, powerful functionality, and extreme ease of use. It is also an open source software that can be found on SourceForge. I can find an article on IBM developerWorks to evaluate the performance, functionality, and usability of mainstream Java XML APIs. dom4j is outstanding in that aspect. Now you can see that more and more Java software are using dom4j to read and write XML. It is particularly worth mentioning that Sun's JAXM is also using dom4j. This is a required jar package, Hibernate uses it to read and write the configuration file dom4j needs to import the dom4j-full.jar package.

Dom4j family member

The main APIs are defined in the org. dom4j package:

1. Attribute defines XML attributes.

2. Branch defines a common behavior for nodes that can contain subnodes, such as XML elements and documents (docuemets,
3. CDATA defines the xml cdata Region
4. CharacterData is an excuse to identify character-based nodes. Such as CDATA, Comment, Text.
5. Comment defines the XML annotation Behavior

6. Document defines the XML Document

7. DocumentType defines the xml doctype Declaration

8. Element defines XML elements

9. ElementHandler defines the processor of the Element object.
10. ElementPath is used by ElementHandler to obtain the path level information currently being processed.

11. Entity defines XML entity
12. Node defines polymorphism for all XML nodes in dom4j

13. NodeFilter defines the behavior of a filter or predicate generated in the dom4j node)

14. ProcessingInstruction defines XML processing instructions.

15. Text defines XML Text nodes.

16. Visitor is used to implement the Visitor mode.

17. After analyzing a string, XPath provides an XPath expression.

How does Dom4j work?

 

1. Create an XML file using dom4j:

1 package com. xml; 2 3 import java. io. fileWriter; 4 import java. io. IOException; 5 6 import org. dom4j. document; 7 import org. dom4j. export enthelper; 8 import org. dom4j. element; 9 import org. dom4j. io. outputFormat; 10 import org. dom4j. io. XMLWriter; 11 12 public class CreateXML {13 14 public static void main (String [] args) {15 XMLWriter writer = null; 16 try {17 // 1.doc ument builder 18 Document doc = Docu MentHelper. createDocument (); 19 // 2. add Element (Root) 20 Element students = doc. addElement ("students"); 21 Element student1 = students. addElement ("student"); 22 // Add attributes; 23 student1.addAttribute ("stuno", "s001"); 24 // Add sub-element 25 student1.addElement ("stuname "). setText ("Liu guanglan"); 26 student1.addElement ("stusex "). setText ("male"); 27 student1.addElement ("stuage "). setText ("25"); 28 // set the output format 29 OutputFormat format = Outp UtFormat. createPrettyPrint (); 30 format. setEncoding ("UTF-8"); 31 writer = new XMLWriter (new FileWrite ("d:/student. xml "), format); 32 writer. write (doc); 33 System. out. println ("XML generated successfully! "); 34} catch (Exception e) {35 e. printStackTrace (); 36} finally {37 try {38 writer. close (); 39} catch (IOException e) {40 // TODO Auto-generated catch block 41 e. printStackTrace (); 42} 43} 44 45} 46 47}

  

 

1 package com. xml; 2 3 import java. io. fileWriter; 4 import java. io. IOException; 5 6 import org. dom4j. document; 7 import org. dom4j. export enthelper; 8 import org. dom4j. element; 9 import org. dom4j. io. outputFormat; 10 import org. dom4j. io. XMLWriter; 11 12 public class CreateXML {13 14 public static void main (String [] args) {15 XMLWriter writer = null; 16 try {17 // 1.doc ument builder 18 Document doc = DocumentHel Per. createDocument (); 19 // 2. add Element (Root) 20 Element students = doc. addElement ("students"); 21 Element student1 = students. addElement ("student"); 22 // Add attributes; 23 student1.addAttribute ("stuno", "s001"); 24 // Add sub-element 25 student1.addElement ("stuname "). setText ("Liu guanglan"); 26 student1.addElement ("stusex "). setText ("male"); 27 student1.addElement ("stuage "). setText ("25"); 28 // set output format 29 OutputFormat format = OutputFormat. createPre TtyPrint (); 30 format. setEncoding ("UTF-8"); 31 writer = new XMLWriter (new FileWrite ("d:/student. xml "), format); 32 writer. write (doc); 33 System. out. println ("XML generated successfully! "); 34} catch (Exception e) {35 e. printStackTrace (); 36} finally {37 try {38 writer. close (); 39} catch (IOException e) {40 // TODO Auto-generated catch block41 e. printStackTrace (); 42} 43} 44 45} 46 47}

 

2. Use dom4j to read an XML file:

 

1 package com. xml; 2 3 import java. io. file; 4 import java. util. list; 5 6 import org. dom4j. document; 7 import org. dom4j. element; 8 import org. dom4j. node; 9 import org. dom4j. io. SAXReader; 10 11 public class ReaderXML {12 public static void main (String [] args) {13 try {14 SAXReader saxReader = new SAXReader (); 15 Document doc = saxReader. read (new File ("d:/student. xml "); 16 // use XPath to access elements, attribute 17 // students/student [1] specify an element 18 // students/student/@ stuno to specify an attribute 19 // students/student [@ stuno = 's002 '] Judge 20 List <Node> data = doc. selectNodes ("/students/student"); 21 for (Node node: data) {22 // node. asXML () Prints XML 23 if ("Element ". equals (node. getNodeTypeName () {24 Element element = (Element) node; 25 // read the attribute value 26 System. out. println (element. attributeValue ("stuno"); 27 // read the child element text value 28 System. out. println (element. elementText ("stuname"); 29 System. out. println (element. elementText ("stusex"); 30 System. out. println (element. elementText ("stuage") + "\ n"); 31} 32} 33} catch (Exception e) {34 e. printStackTrace (); 35} 36} 37 38}

 

 

1 package com. xml; 2 3 import java. io. file; 4 import java. util. list; 5 6 import org. dom4j. document; 7 import org. dom4j. element; 8 import org. dom4j. node; 9 import org. dom4j. io. SAXReader; 10 11 public class ReaderXML {12 public static void main (String [] args) {13 try {14 SAXReader saxReader = new SAXReader (); 15 Document doc = saxReader. read (new File ("d:/student. xml "); 16 // use XPath to access elements, attribute 17 // students/student [1] specify an element 18 // students/student/@ stuno to specify an attribute 19 // students/student [@ stuno = 's002 '] Judge 20 List <Node> data = doc. selectNodes ("/students/student"); 21 for (Node node: data) {22 // node. asXML () Prints XML23 if ("Element ". equals (node. getNodeTypeName () {24 Element element = (Element) node; 25 // read the attribute value 26 System. out. println (element. attributeValue ("stuno"); 27 // read the child element text value 28 System. out. println (element. elementText ("stuname"); 29 System. out. println (element. elementText ("stusex"); 30 System. out. println (element. elementText ("stuage") + "\ n"); 31} 32} 33} catch (Exception e) {34 e. printStackTrace (); 35} 36} 37 38}

 


 

3. Use dom4j to modify an XML file:

 

1 package com. xml; 2 3 import java. io. file; 4 import java. io. fileWriter; 5 import java. io. IOException; 6 import java. util. list; 7 8 import org. dom4j. document; 9 import org. dom4j. element; 10 import org. dom4j. node; 11 import org. dom4j. io. outputFormat; 12 import org. dom4j. io. SAXReader; 13 import org. dom4j. io. XMLWriter; 14 15 public class UpdateXMLData {16 17 public static void main (String [] args) {1 8 XMLWriter writer = null; 19 try {20 SAXReader saxReader = new SAXReader (); 21 Document doc = saxReader. read (new File ("d:/student. xml "); 22 List <Node> data = doc 23. selectNodes ("/students/student [@ stuno = 's002 ']"); 24 for (Node node: data) {25 if ("Element ". equals (node. getNodeTypeName () {26 Element temp = (Element) node; 27 // modify the stuno attribute value of student 28 temp. setAttributeValue ("stuno", "stu002 "); 29} 30} 31 OutputFormat format = OutputFormat. createPrettyPrint (); 32 format. setEncoding ("UTF-8"); 33 writer = new XMLWriter (new FileWriter ("d:/student. xml "), format); 34 writer. write (doc); 35 System. out. println ("modified successfully! "); 36} catch (Exception e) {37 e. printStackTrace (); 38} finally {39 try {40 writer. close (); 41} catch (IOException e) {42 // TODO Auto-generated catch block 43 e. printStackTrace (); 44} 45} 46 47} 48 49}

 


This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/yueye729107/archive/2010/01/29/5269887.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.