Java Get XML node summary of methods for reading XML document nodes _java

Source: Internet
Author: User

DOM4J is a Java XML API used to read and write XML files. There are many scenarios in which XML is read and written using DOM4J.

To use DOM4J development, you need to download the appropriate jar file for the import dom4j.

Website Download: HTTP://WWW.DOM4J.ORG/DOM4J-1.6.1/

GitHub Download: http://dom4j.github.io/

After decompression, as shown in the figure:

We just need to build the Dom4j-1.6.1.jar file into our development project.

The following is an example of how eclipse creates a Java project:

Disclaimer: Development environment for this Java Project Jdk1.8,eclipse version neon.1 release (4.6.1)

First create a demo project:

Create a Lib file in the demo project, copy the Dom4j-1.6.1.jar file to Lib, and then right-click Dom4j-1.6.1jar file. As shown in the figure:

Click Add to Bulid path to build into the project.
The import was successful as shown in the following illustration:

In the process of project development, you can refer to the Docs folder (Help documentation), find the index.html open, click Quick Start to learn dom4j XML parsing through the Help document.
As shown in the figure:

Below I will take a detailed example to introduce Java operation XML file, file name is Exmple.java.

Package vastsum;
Import Java.io.File;
Import Java.util.Iterator;

Import java.util.List;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import org.dom4j.Element;
Import Org.dom4j.Node;

Import Org.dom4j.io.SAXReader;
 /** * Reads the XML document and obtains the Document object.
 * This article is the first XML serialization, the following code can be run directly, the end of the source download address attached. * * Class Exmple {public static void main (string[] args) throws documentexception {Saxreader reader = new Saxread
    ER ();
    
    Document document = Reader.read (New File ("./src/contact.xml"));
    Operation Method of/** * Node Object *//Get document root node Element root = Document.getrootelement ();
    
    
    The name of the output root tag System.out.println (Root.getname ());
    Gets all child nodes below the root node (child nodes that do not have child nodes) list<element> List = Root.elements ();
    Traversal list method for (Element e:list) {System.out.println (E.getname ()); 
    //Get the child node Element Contactelem = root.element ("Contact") under the specified node;/First know the node you want to manipulate.
    List<element> contactlist = contactelem.elements (); For (Element e:contactlist) {System.out.println (E.getname ());
    //Call to get the recursive function of the child node below.
    
    
    Getchildnodes (root);
    Gets the first child label of the specified name under the current label Element Conelem = root.element ("contact");
    
    
    System.out.println (Conelem.getname ());
    Get a deeper label (one layer of fetch) element Nameelem = root.element ("contact"). Element ("name");
  System.out.println (Nameelem.getname ());
    }//Recursive query node function, output node name private static void Getchildnodes (Element elem) {System.out.println (Elem.getname ());
    Iterator<node> it= elem.nodeiterator ();
      while (It.hasnext ()) {node node = It.next ();
        if (node instanceof Element) {element e1 = (element) node;
        Getchildnodes (E1); }
      
      }
    }
  
  
}

Here is the corresponding XML file named Contact.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<contactList>
  <contact id= "001" class= "Style" >
    <name> John </name>
    <age>20</age>
    <phone>134222223333</phone>
    <email>zhangsan@qq.com</email>
    <qq>432221111</qq>
  </contact>
  <contact id= "002" >
    <name> Dick </name>
    <age>20</age>
    <phone> 134222225555</phone>
    <email>lisi@qq.com</email>
    <qq>432222222</qq>
  </contact>
  <contactTwo>
    <name> Harry </name>
    <age>32</age>
    <phone>465431341</phone>
    <emali>af@qq.com</emali>
    <qq>46164694 </qq>
  </contactTwo>
  <test> test </test>
  <test> other uses </test>
</contactList>

The directory of two files is as follows:

The above is a small series for you to bring Java to get XML node summary of reading XML document node method all the content, I hope that we support cloud-Habitat Community ~

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.