Java Parsing XML Encyclopedia

Source: Internet
Author: User

First, the preface

There are two most common ways to parse XML documents in Java: Using the event-based XML Simple API (simply API for XML), called Sax and the Document object model based on the tree and node, is called Dom. Sun offers Java API for XML parsing (JAXP) interfaces to use Sax and Dom, and with JAXP, we can use any of the JAXP-compliant XML parsers.

The Jaxp interface contains three packages:

(1) Org.w3c.dom the interface recommended for the XML standard planning Document Object model.

(2) Org.xml.sax event-driven XML Simple API (SAX) for parsing xml

(3) Javax.xml.parsers Parser Factory tool, the programmer obtains and configures special special parser.

Second, the premise

Dom programming does not include other dependencies because the Org.w3c.dom, Org.xml.sax, and javax.xml.parsers packages included with the JDK in the JDK can satisfy the criteria.

III. parsing XML documents using DOM

Let's now look at how the DOM parses XML. Again, I'll explain how the DOM parses XML documents from a simple, no more simple example: Let's take a look at what XML is.

View plain Copy to clipboard print? <?xml version= "1.0" encoding= "gb2312"?> <books> <book email= "zhoujunhui@163.com" > <name> Java ideas </name> <price>99.0</price> </book> </books>

The simple can't be simpler. But that's all there is, the root element, the attribute, the child node. Well, it's OK to respond to the problem, let's look at the Java code to parse the XML file.

  View plain Copy to clipboard print? import java.io.fileinputstream;   import java.io.filenotfoundexception;   Import  java.io.IOException;   import java.io.inputstream;   import  javax.xml.parsers.documentbuilder;   import javax.xml.parsers.documentbuilderfactory;    import javax.xml.parsers.parserconfigurationexception;   import org.w3c.dom.document;    import org.w3c.dom.element;   import org.w3c.dom.node;   import  org.w3c.dom.nodelist;   import org.xml.sax.saxexception;      public  class domparse {       public domparse () {            documentbuilderfactory domfac=documentbuilderfactory.newinstance ();            try {   &NBSP;&NBsp;          documentbuilder dombuilder= Domfac.newdocumentbuilder ();                inputstream is=new fileinputstream ("Webroot/web-inf/hell.xml");                document doc=dombuilder.parse (IS);               element root= Doc.getdocumentelement ();                Nodelist books=root.getchildnodes ();                if (books!=null) {                    for (Int i=0;i<books.getlength (); i++) {                        node book=books.item (i);                        if ( Book.getnodetype () ==node.element_node) {                            string email= Book.getattributes (). getNamedItem ("email"). Getnodevalue ();                             SYSTEM.OUT.PRINTLN (email);                            for (Node node=book.getFirstChild ( ); node!=null;node=node.getnextsibling () {                                 if (node.getNodeType () = = Node.element_node) {                                     if (Node.getnodename (). Equals ("name")) {                                         string name=node.getnodevalue ();                                         string  name1=node.getfirstchild () getnodevalue ();                                  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (name);                                         system.out.println (name1);                                     }                                     if (Node.getNodeName (). Equals (" Price ")) {                                         String price=node.getfirstchild () getnodevalue ();                              &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (price);                                     }                                 }                            }                        }                   }               }            } catch  (parserconfigurationexception e)  {                e.printstacktrace ();            } catch  (filenotfoundexception e)  {                e.printstacktrace ();            } catch  (saxexception e)  {                e.printstacktrace ();            } catch  (ioexception e)  {                e.printstacktrace ();            }       }       public static void  main (String[] args)         {            new domparse ();       }          }    

Iv. Code Interpretation

Let's take a look at the program reference class:

Import Java.io.FileInputStream;

Import java.io.FileNotFoundException;

Import java.io.IOException;

Import Java.io.InputStream;

Import Javax.xml.parsers.DocumentBuilder;

Import Javax.xml.parsers.DocumentBuilderFactory;

Import javax.xml.parsers.ParserConfigurationException;

The following are mainly Org.xml.sax package classes

Import org.w3c.dom.Document;

Import org.w3c.dom.Element;

Import Org.w3c.dom.Node;

Import org.w3c.dom.NodeList;

Import org.xml.sax.SAXException;

The above simple code can be seen, but in order to introduce DOM programming, you might want to take a look at this program:

(1) A factory instance that gets a DOM parser

Documentbuilderfactory domfac=documentbuilderfactory.newinstance ();

The instance of the Javax.xml.parsers.DocumentBuilderFactory class is the parser factory we want

(2) Get DOM parser from Dom factory

Documentbuilder Dombuilder=domfac.newdocumentbuilder ();

The DOM parser is Newdocumentbuilder () by the static method of Javax.xml.parsers.DocumentBuilderFactory instance

(3) Convert the XML document to be parsed into an input stream so that the DOM parser can parse it

InputStream is=new FileInputStream ("Bin/library.xml");

InputStream is an interface.

(4) Parsing XML document input stream, getting a document

Document Doc=dombuilder.parse (IS);

A Org.w3c.dom.Document object is obtained from the input stream of an XML document, and subsequent processing is done on the Document object

(5) Get the root node of XML document

Element root=doc.getdocumentelement ();

Only the root node in the DOM is a Org.w3c.dom.Element object.

(6) To get the node's child nodes

NodeList Books=root.getchildnodes ();

for (int i=0;i<books.getlength (); i++) {

Node Book=books.item (i);

}

This is a org.w3c.dom.NodeList interface to store all of its child nodes, there is a round of sub-node method, followed by the introduction

(7) Get the attribute value of the node

String email=book.getattributes (). getNamedItem ("email"). Getnodevalue ();

SYSTEM.OUT.PRINTLN (email);

Notice that the property of the node is also its child node. Its node type is also Node.element_node

(8) Round-robin nodes

For (Node node=book.getfirstchild (); node!=null;node=node.getnextsibling ()) {

if (Node.getnodetype () ==node.element_node) {

if (Node.getnodename (). Equals ("name")) {
String Name=node.getnodevalue ();

String name1=node.getfirstchild (). Getnodevalue ();

SYSTEM.OUT.PRINTLN (name);

System.out.println (NAME1);

}

if (Node.getnodename (). Equals ("price")) {

String price=node.getfirstchild (). Getnodevalue ();

SYSTEM.OUT.PRINTLN (price);
}

}

The printout of this code is:

Zhoujunhui@163.com
Null
Java
99.0

It can be seen from above

String Name=node.getnodevalue (); is a null value. and

String name1=node.getfirstchild (). Getnodevalue (); is the real value, because DOM treats <name>rjzjh</name> as a two-tier node with its parent node as the <name> node itself, and it has only one child node (if there are more than one). , the child node is its value "RJZJH", so we see the result above.

Also, the node type of the child node is also node.element_node type, and the Node.getnextsibling () method is to remove an adjacent node.

v. DOM nodes

The DOM is a collection of nodes that have several different types of nodes defined because the document may contain different types of information. The most common types of nodes in the DOM are:

(1) Element:

An element is the basic component of XML. The child nodes of an element can be other elements, text nodes, or both. ELEMENT nodes can also contain only nodes of this unique type of attribute.

(2) Properties:

The attribute node contains information about the element node, but it is not a child of the element

(3) Text:

Text node text information, or simply blank text.

(4) Documentation:

The document node is the parent of all other nodes in the entire document

An element is a very important type of node, and an element node can be a container for other nodes.

Six, method of string conversion to XML

view Plaincopy to Clipboardprint? First, the use of the most primitive javax.xml.parsers, the standard jdk api  //  string to xml   string xmlstr =  /".../";   Stringreader sr = new stringreader (XMLSTR);   Inputsource is = new inputsource (SR);   documentbuilderfactory factory  = documentbuilderfactory.newinstance ();   documentbuilder builder= Factory.newdocumentbuilder ();   document doc = builder.parse (IS);  //xml spin string    transformerfactory  tf  =  transformerfactory.newinstance ();    Transformer t = tf.newtransformer ();   t.setoutputproperty (/"encoding/",/" gb23121/")/solve Chinese problem, try to use GBK    bytearrayoutputstream  bos  =  new   bytearrayoutputstream ();   t.transform (New domsource (DOC),  new streamresult (BOS) );   string xmlstr =&Nbsp;bos.tostring ();      documentbuilderfactory factory =  Documentbuilderfactory.newinstance ();      DocumentBuilder builder;      try    {      builder  =   Factory.newdocumentbuilder ();      Document doc  =   Builder.parse ( new  bytearrayinputstream (str.getbytes));      }    catch   (parserconfigurationexception e)    {        //  TODO Auto-generated catch block         e.printstacktrace ();     }   catch   (saxexception e)    {       //  TODO Auto-generated catch  block        e.printstacktrace ();     }   catch   (ioexception  e)    {       //  todo auto-generated catch  block        e.printstacktrace ();     }     Here the xml document for org.w3c.dom.document   II, the use of dom4j after the program becomes simpler   //  string to xml    string xmlstr = /".../";   document document =  Documenthelper.parsetext (XMLSTR);  // xml turn string    document document =  ;   string text = document.asxml ();      Saxreader saxreader  = new saxreader ();           Document  document;           try {                 document = saxreader.read (New bytearrayinputstream ( Str.getbytes ());               element  incomingform = document.getrootelement ();            } catch  (documentexception e)  {                // TODO Auto-generated catch block               e.printstacktrace ();            }   here the xml document for org.dom4j.document   three, Using jdom   Jdom and the first method of handling very similar to   //string to xml   string xmlstr = /"... ";   Stringreader sr = new stringreader (xmlstr);   InputSource is  = new  InputSource (SR);   document doc =  (New saxbuilder ()). Build (IS);  // XML spin string    Format format = format.getprettyformat ();   format.setencoding (/" gb2312/")//configure XML document characters as gb2312, resolve Chinese issues    Xmloutputter xmlout = new xmloutputter ( Format);   Bytearrayoutputstream bo = new bytearrayoutputstream ();   Xmlout.output (Doc,bo);   string xmlstr = bo.tostring ();   here xml  Document for org.jdom.document   IV, processing in JavaScript   //String xml   var xmlstr =  /".../";   var xmldoc = new activexobject (/"microsoft.xmldom/");    xmldoc.async=false;   xmldoc.loadxml (XMLSTR);  //able to handle this xmldoc    var  Name = xmldoc.selectsinglenode (/"/person/name/");   alert (name.text);  //xml Turn string    Var xmldoc = ......;    var xmlstr = xmldoc.xml   Here's xml document for JavaScript version of XMLDOM  

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.