Dom4j creates and parses xml documents and dom4jxml documents

Source: Internet
Author: User

Dom4j creates and parses xml documents and dom4jxml documents

DOM4J Parsing

Features:

1. A smart branch of JDOM, which combines many functions beyond the representation of basic XML documents.

2. It uses interfaces and abstract basic class methods.

3. excellent performance, good flexibility, powerful functions, and extreme ease of use.

4. It is an open source file.

Jar: dom4j-1.6.1.jar

Create book. xml:

1 package com. example. xml. dom4j; 2 3 import java. io. fileWriter; 4 import org. dom4j. document; 5 import org. dom4j. export enthelper; 6 import org. dom4j. element; 7 import org. dom4j. io. outputFormat; 8 import org. dom4j. io. XMLWriter; 9/** 10 * dom4j create xml document example 11*12 */13 public class Dom4JTest4 {14 public static void main (String [] args) throws Exception {15 // Method 2: create a document and set the root Element node of the document as 16 Element root2 = javasenthelper. createElement ("bookstore"); 17 Document document2 = incluenthelper. createDocument (root2); 18 19 // add a level-1 subnode: After adding, this Element 20 Element book1 = root2.addElement ("book"); 21 book1.addattriement ("id ", "1"); 22 book1.addattriement ("name", "first book"); 23 // Add Level 2 subnode 24 book1.addElement ("name "). setText ("encounter unknown yourself"); 25 book1.addElement ("author "). setText ("Zhang defen"); 26 book1.addElement ("year "). setText ("2014"); 27 book1.addElement ("price "). setText ("109"); 28 // Add level-1 subnode 29 Element book2 = root2.addElement ("book"); 30 book2.addattriement ("id", "2 "); 31 book2.addattriement ("name", "Second Book"); 32 // Add a second-level subnode 33 book2.addElement ("name "). setText ("Two Cities"); 34 book2.addElement ("author "). setText ("Charles"); 35 book2.addElement ("year "). setText ("2007"); 36 book2.addElement ("price "). setText ("29"); 37 38 // set indent to 4 spaces, and the next line is true39 OutputFormat format = new OutputFormat ("", true ); 40 41 // another output method. Remember to call the flush () method. Otherwise, the output file contains a blank space. 42 XMLWriter xmlWriter3 = new XMLWriter (new FileWriter ("book. xml "), format); 43 xmlWriter3.write (document2); 44 xmlWriter3.flush (); 45 // close () method can also be 46 47} 48}

Running result:

Parse book. xml:

1 package com. example. xml. dom4j; 2 3 import java. io. file; 4 import java. util. iterator; 5 import java. util. list; 6 import org. dom4j. attribute; 7 import org. dom4j. document; 8 import org. dom4j. extends entexception; 9 import org. dom4j. element; 10 import org. dom4j. io. SAXReader; 11/** 12 * dom4j parsing xml document example 13*14 */15 public class Dom4JTest3 {16 17 public static void main (String [] args) {18 // parse books. xml file 19 // Create the SAXReader object reader20 SAXReader reader = new SAXReader (); 21 try {22 // use the read method of the reader object to load the books. xml file and obtain the docuemnt object. 23 Document document = reader. read (new File ("book. xml "); 24 // get the root node bookstore25 Element bookStore = document through the document object. getRootElement (); 26 System. out. println ("root node name:" + bookStore. getName (); 27 // get Iterator 28 Iterator it = bookStore through the elementIterator method of the element object. elementIterator (); 29 // traverses the iterator to obtain information (books) in the root node 30 while (it. hasNext () {31 System. out. println ("==== start traversing subnodes ===="); 32 Element book = (Element) it. next (); 33 System. out. println ("subnode name:" + book. getName (); 34 // get the Attribute name and Attribute value of book 35 List <Attribute> bookAttrs = book. attributes (); 36 for (Attribute attr: bookAttrs) {37 System. out. println ("attribute name:" + attr. getName () + "-- property value:" 38 + attr. getValue (); 39} 40 Iterator itt = book. elementIterator (); 41 while (itt. hasNext () {42 Element bookChild = (Element) itt. next (); 43 System. out. println ("node name:" + bookChild. getName () + "-- node value:" + bookChild. getStringValue (); 44} 45 System. out. println ("==== end traversing this node ===="); 46} 47} catch (incluentexception e) {48 e. printStackTrace (); 49} 50} 51 52}

Running result:

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.