DOM4J parsing and generating XML documents

Source: Internet
Author: User
Tags gettext
First, the preface

DOM4J is a very good Java open source API, mainly used to read and write XML documents, with excellent performance, powerful, and very convenient to use the characteristics. In addition, XML is often used for data exchange carriers, such as calling WebService pass parameters, and data synchronization operations, etc., so using DOM4J parsing XML is very necessary.


Ii. Conditions of preparation

Dom4j.jar

Download Address: http://sourceforge.net/projects/dom4j/


third, the use of dom4j combat


1. Parsing XML documents

Realize the idea:

<1> returns a Document object after being passed to Saxreader based on the read XML path;

<2> then manipulate the document object to obtain the following node and the child node information;

The specific code is as follows:

[Java]  View plain  copy     import java.io.file;   import java.io.fileinputstream;    import java.io.inputstream;   import java.util.iterator;   Import  java.util.List;      import org.dom4j.document;   import  org.dom4j.documenthelper;   import org.dom4j.element;   import  org.dom4j.io.saxreader;     /**   *  using DOM4J to parse XML documents    *  @author  Administrator   *   */   public class dom4jparsexmldemo {               public void parsexml01 () {            try{                //converts the XML below SRC into an input stream                &nBsp;inputstream inputstream = new fileinputstream (New file ("D:/project/dynamicWeb/src /resource/module01.xml ");                 //inputstream inputstream = this.getclass (). getResourceAsStream ("/module01.xml"); You can also find xml               //based on the class's compiled file relative path. Create Saxreader Reader, specifically for reading xml                Saxreader saxreader = new saxreader ();                //According to Saxreader's read rewrite method, it can be read either through the InputStream input stream or through the file object to read the                  //document document =  saxreader.read (inputstream);                  dOcument document = saxreader.read (New file ("d:/project/dynamicweb/src/resource/ Module01.xml "))//must specify the absolute path of the file                 //It is also possible to use the XML converter provided by Documenthelper.                //document document  = documenthelper.parsetext ("<?xml version=\" 1.0\ " encoding=\" UTF-8\ "><modules"?  id=\ "123\" ><module>  this is the module label text Message </module></modules> ");                                //get root Node object                 element rootelement = document.getrootelement ();                  system.out.println("Root node Name:"  + rootelement.getname ());//Get the name of the node                 system.out.println ("How many properties of the root node:"  + rootelement.attributecount ()); Gets the number of node properties                system.out.println ("Value of the root Node id attribute:"  + rootelement.attributevalue ("id"));//Get the value of the node's property ID                 system.out.println (text within the root node:  + rootelement.gettext () //If the element has child nodes, returns an empty string, otherwise returns the text within the node                 //rootelement.gettext ()   is wrapping because the tab and line breaks are used between the   label and the label, which is also a text that shows the effect of wrapping the line.   

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.