Public class domparse {
Public domparse (){
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
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. 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 );
}
}
}
}
}
}
} 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 explanation
Let's take a look at the reference class of this program:
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 is the class of the org. xml. Sax package.
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 understood at a glance, but to introduce a DOM programming, let's take a look at this program:
(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
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 );
}
}
The output of this Code 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, because Dom regards zookeeper as a two-layer node, and its parent node is the node itself, and it only has one subnode (if there is an attribute, there will be more than one !), The sub-node is its value "zoozookeeper", so we can see the above result.
In addition, the node type of the subnode is also node. element_node, And the node. getnextsibling () method is to take the next adjacent node.
V. DOM nodes
Dom is a collection of nodes. Since the document may contain different types of information, several different types of nodes are defined. The most common node types in Dom are:
(1) elements:
Element is the basic component of XML. The child nodes of an element can be other elements, text nodes, or both. An element node can also contain only nodes of the unique attribute type.
(2) attributes:
An attribute node contains information about an element node, but it is not a child node of an element.
(3) text:
Text node text information, or empty text.
(4) Documentation:
The document node is the parent node of all other nodes in the document.
Element is a very important type node. Element nodes can be containers of other nodes.
6. Steps for Dom to parse XML documents:
For the main steps, see steps (1), (2), (3), and (4) at the fourth point.