Java parses XML documents -- DOM parses XML

Source: Internet
Author: User
Tags xml parser
I. Preface

There are two common methods to parse XML documents using Java: Simple API for XML) the Document Object Module, known as Sax and the Document Object Model Based on trees and nodes, is called Dom. Sun provides the Java API for XML parsing (JAXP) interface to use SAX and Dom. Through JAXP, we can use any XML Parser compatible with JAXP.

The JAXP interface contains three packages:

(1) The interface recommended by org. W3C. Dom W3C for the XML standard Planning Document Object Model.

(2) event-driven XML Simple API (SAX) for syntax analysis of XML)

(3) javax. xml. parsers parser factory tool, Program The operator obtains and configures a special syntax analyzer.

Ii. Prerequisites

Do not use other dependency packages for Dom programming, because the JDK contains the org. w3C. dom, org. XML. sax and javax. XML. the parsers package can meet the requirements.

Iii. Use Dom to parse XML documents

Let's take a look at how Dom parses XML! Similarly, I will explain how Dom parses XML documents from a simple example. Let's take a look at what XML is:
<? XML version = "1.0" encoding = "gb2312"?>

<Books>

<Book email = "zhoujunhui">

<Name> zhangzwon </Name>

<Price> jjjjjj </price>

</Book>

</Books>

Public void usedomparsexml (){
Documentbuilderfactory domfac = documentbuilderfactory. newinstance ();

Try {
Documentbuilder dombuilder = domfac. newdocumentbuilder ();
Inputstream is = new fileinputstream ("bin/library. 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 = book. getfirstchild (); node! = NULL;
Node = node. getnextsibling ()){
If (node. getnodetype () = node. element_node ){
If (node. getnodename (). Equals ("name ")){
String name = node. getfirstchild ().
Getnodevalue ();
System. Out. println (name );
}

If (node. getnodename (). Equals ("price ")){
String price = node. getfirstchild ().
Getnodevalue ();
System. Out. println (price );
}
}
}
}
}
}
} Catch (exception e ){

}
}

(1) Get the factory instance of the DOM parser

Documentbuilderfactory domfac = documentbuilderfactory. newinstance ();

Get javax. xml. parsers. documentbuilderfactory; the instance of the class is the parser factory we want.

(2) Get the DOM parser from the DOM Factory

Documentbuilder dombuilder = domfac. newdocumentbuilder ();

Use the static method newdocumentbuilder () of the javax. xml. parsers. documentbuilderfactory instance to obtain the DOM parser.

(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) parse the input stream of the XML document to obtain a document

Document Doc = dombuilder. parse (is );

An org. W3C. Dom. Document Object is obtained from the input stream of the XML document. Subsequent processing will be performed on the document object.

(5) Get the root node of the XML document

Element root = Doc. getdocumentelement ();

In Dom, only the root node is an org. W3C. Dom. Element Object.

(6) obtain the subnode of the node

Nodelist books = root. getchildnodes ();

For (INT I = 0; I <books. getlength (); I ++ ){

Node book = books. item (I );

}

This is to use an org. W3C. Dom. nodelist interface to store all its subnodes. There is also a method to round-robin subnodes, which will be introduced later.

(7) Get the node Attribute Value

String email = book. getattributes (). getnameditem ("email"). getnodevalue ();

System. Out. println (email );

Note that the attributes of a node are also its subnodes. Its node type is also node. element_node

(8) Round Robin subnode

For (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 );
}

}

This section Code The output is:

Null

Alterrjz.pdf

Jjjjjj

We can see from the above

String name = node. getnodevalue (); is a null value. While

String name1 = node. getfirstchild (). getnodevalue (); is the real value. This is because Dom regards <Name> zoozookeeper </Name> as a two-layer node.

Related Article

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.