Java EE XML Dom parsing

Source: Internet
Author: User

DOM Parsing xml

@author Ixenos

XML parsing method (principle)

A) DOM parse tree

b) SAX Stream events

Dom parsing corresponds to the main tool

I. DOM (official)

Ii. dom4j (unofficial mainstream three frameworks using dom4j parsing configuration files)

Iii. JDOM (unofficial)

Sax parsing corresponds to mainstream tools

SAX (Official mainstream)

Parsing XML documents

A) to process an XML document, parse it first,

b) Parser program: read in a file, confirm that the file has the correct format, and then break it down into various elements, so that the programmer can access these elements

I. Document Object Model (Doc object model,dom) parser, a tree-type parser (parser) that transforms a read-in XML document into a tree structure

Ii. XML Simple API (easy API for Xml,sax) parser, which is a stream mechanism parser (streaming parser) that generates the corresponding event when it is read into the XML document

c) If you are only interested in certain elements and don't care about their context, then in these cases you should consider using the flow mechanism parser

Nodes node

Only one root node in an XML document

Element node with no parent node

ELEMENT node

All tags in the root node and root node are element nodes

Attribute node

<student id= "001" ></student> ID is the attribute node

Text node

<name>zhangsan</name> Zhangsan is the text node

Comment Node

<!----> comment information in XML documents

Element type: <a>b</a> with label, node name is a, nodes value is null, B is node value, and child node (at which point B is a node of type text)

attr Type: node value is attribute name, node value is property value

Text type: Outside of the label is a literal node, the node name is #text, the value is text

Therefore, we believe that the "node value" of a node of type element, which is actually a child of the text type of the element type node-refers to the content sandwiched between the label and cannot be obtained through Getnodevalue (), by:

    1. Getfirstchild (). Getnodevalue () Gets the node value of the first child node
    2. Gettextcontent () Gets the node value of all the text subnodes , and also recursively gets the node value of the grandchild node.

A) For example <a><b>bb</b> hahaha </a> will get: BB hahaha

b) Instead of 1. Will parse the element node of B, will get null

DOM Official parsing example

A) The first thing to understand is that XML is considered a document, so we need a Documentbuilder object to read the XML and generate the corresponding paper object

I./* Generate builder*/with builder factory

II. Documentbuilderfactory factory = Documentduilderfactory.newinstance ();

III. Documentbuilder builder = Factory.newdocumentbuilder ();

Iv./* Generate Document object with builder read XML document */

V. File F = new file ("...");

VI. Document doc = Builder.parse (f); You can also read the URL or the input stream

b) Start analysis of document, call getdocumentelement, get root element

I. Element root = Doc.getdocumentelement ();

c) Get a list of the root nodes node,

I. Note that the child node has not only element type and text text type (not in the label, even the blank of the newline carriage return is counted )

II. NodeList children = Root.getchildnodes ();

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

Node child = Children.item (i);

......

Iv.}

V. If you only want to get the element child nodes (elements), you can omit the white space character (Text):

VI. NodeList children = Root.getchildnodes ();

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

Node child = Children.item (i);

if (child instanceof Element) {

Element childelement = (element) child;

...

}

VIII.}

D) If the document has a DTD, then the parser knows what is not a child element of the text node, and automatically rejects the white space character! The DTD is very useful

e) such as <a>123</a><b>456</b> when we want to know the information that the label is holding, since text is the only child node of the child nodes of these elements , You can use the Getfirstchild method without having to walk through the nodelist of the child nodes of these elements, and then only need to getdata the text node to get the string.

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

    1. Node child = Children.item (i);
    2. If (Child instanceof Element) {

a) Element childelement = (element) child;

b) Text Textnode = (text) childelement.getfirstchild ();

c) String text = Textnode.getdata (). Trim (); Remove extra spaces and line breaks

d) if (Childelement.gettagname (). Equals ("name"))//name label signature

I. name = text;

e) Else if (Childelement.gettagname (). Equals ("size"))

I. Size = Integer.parseint (text);

}

}

After getdata the text child nodes of the element nodes, you can optimize this situation:

<size>

36

</size>

In this case, the text child node contains line breaks and spaces, and the call trim can delete the space before and after

F) Enumeration element child node properties <size hel= "PT" >36</size>

I. A label can have multiple properties, each with its own name and value, for example:

<input name= "Text" >

Attribute values must be enclosed in double quotation marks (") or single quotation marks (').

Defining attributes must follow the same naming conventions as labels

Learn more: In XML technology, the information represented by the tag attribute can also be changed to describe it in the form of a child element, for example:

<input>

<name>text</name>

</input>

Note: GetAttributes is not available at this time.

II. The Document object calls GetAttributes, which returns a Namenodemap object that contains the node object that describes the property (key-value pair), iterates over the map node, calls Getnodename and Getnodevalue to get the property name and the property value

III. or know the property name and get the property value directly

    1. String unit = element.getattrbute ("unit"); Unit is the property name, assigning the property value to the string variable unit

Or

External

<! DOCTYPE configuration Public "HTTP://MYSERVER.COM/CONFIG.DTD" >

Java EE XML Dom parsing

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.