Import java.io.IOException;
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.NamedNodeMap;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException; /** * Use DOM to parse XML file * @author Admin * * * * public class Main {public static void main (string[] args) {//Create a Documen
The object of tbuilderfactory documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
try {//Create Documentbuilder object Documentbuilder db = Dbf.newdocumentbuilder ();
Loading book.xml to the current directory via the parse method of the Documentbuilder object document = Db.parse ("Demo/book.xml");
Gets the collection of all book nodes nodelist Booklist = document.getElementsByTagName ("book");
The length System.out.println of Booklist is obtained by means of the GetLength () method of NodeList ("There are:" +booklist.getlength () + "book"); for (int i=0;i<booklist.getlength (); i++) {System.out.println ("Begin traversing the first" + (i+1) + "The contents of this book");
Get a book node by the item (i) = Booklist.item (i);
Gets a collection of all the properties of the book node NamedNodeMap attrs = Book.getattributes ();
System.out.println ("The first" + (i+1) + "This book has a total of" +attrs.getlength () + "attributes"); Iterate through the book's properties for (int j=0;j<attrs.getlength (), j + +) {//Gets a property of the book node attr = Attrs.item (j) through the item (index) method.
);
Gets the property name System.out.print ("Property name:" +attr.getnodename () + ",");
Gets the property value System.out.println ("Property value:" +attr.getnodevalue ()); The * * * can be traversed with the following method when knowing the value of the book's property, and there is only one ID attribute * To cast the book node to the element type element book = (eleme
NT) Booklist.item (i);
String AttrValue = Book.getattribute ("id");
SYSTEM.OUT.PRINTLN ("id's property value is" +attrvalue);
*//parse the child node of the book node nodelist childnodes = Book.getchildnodes ();
Traversal childnodes Gets the node name and node value of each node System.out.println ("the first" + (i+1) + "This book has" +childnodes.getlength () + "child Node"); for (int k=0;k<childnodes.getlength (); k++) {//distinguish between node of text type and element class-Type node if (Childnodes.item (k). Getnodetype () ==node.element_node) {//Gets the node name System.out.print ("node
Name: "+childnodes.item (k). Getnodename () +", ");
System.out.println ("Node value:" +childnodes.item (k). Getfirstchild (). Getnodevalue ());
System.out.println ("Node value:" +childnodes.item (k). Getfirstchild (). Gettextcontent ());
catch (Parserconfigurationexception e) {e.printstacktrace ()}}};
catch (Saxexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}
}
}
Book.xml structure
<?xml version= "1.0" encoding= "UTF-8"?>
<bookstore>
<book id= "1" >
<name> Anderson fairy tale </name>
<author> Anderson </author>
<year>2016</year>
<page>521</ page>
</book>
<book id= "2" >
<name> famous Detective Conan </name>
<author> Qingshan Chang </author>
<year>1996</year>
<page>5412</page>
</book>
</bookstore>
Output results
Altogether there are: 2 books
The following begins to traverse the 1th book
The 1th book has a total of 1 properties
Property name: ID, property value: 1
The 1th book has 9 sub nodes
Section call: Name, node value: Anderson fairy tale
Section name: Author, node value: Anderson
Section name: Year, node value: 2016
Section Roll Name: page, node value: 521
The following begins to traverse the 2nd book
The 2nd book has a total of 1 properties
Property name: ID, property value: 2
The 2nd book has 9 sub nodes
Section name: Name, node value: Detective Conan
Section name: Author, node value: Qingshan Chang
Section name: Year, node value: 1996
Section Roll Name: page, node value: 5412
The writer : Dream lei @ Mailbox
copyright Notice unless noted, this article Yu Menglei original compilation
Reprint Please indicate the source: http://zhiml.cn/index.php/post/40.html