Package domtest;
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.NamedNodeMap;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;
public class Domtest {
public static void Main (string[] args) {
TODO auto-generated Method Stub
To create an Documentbuilderfactory object
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
try {
Create a Documentbuilder object
Documentbuilder db = Dbf.newdocumentbuilder ();
Parsing an XML file by using the parse (String fileName) method of the Documentbuilder object
Use the parse () method to load the Books.xml file into the current project
Document document= db.parse ("books.xml");
Gets the collection of all books nodes
NodeList Booklist = document.getElementsByTagName ("book");
Booklist method is obtained through the GetLength method of Booklist.
System.out.println ("A total" + booklist.getlength () + "Book! ");
facilitates each book node
for (int i = 0; i < booklist.getlength (); i++) {
System.out.println ("===================== begins to traverse the" + (i + 1) + "contents of this book =============================");
Gets a book node through the item (i) method, with the index value of nodelist starting from zero
Node book= Booklist.item (i);
Gets the collection of all properties of the book node
NamedNodeMap att = book.getattributes ();
System.out.println ("No." + (i + 1) + "book Total" + att.getlength () + "Properties! ");
Traversing the properties of a book
for (int j = 0; J < Att.getlength (); j + +) {
Gets a property of the book node through the ATT's Item method
Node attr= Att.item (j);
Get Property name
System.out.print ("attribute name:" + attr.getnodename ());
Get property value
SYSTEM.OUT.PRINTLN ("---attribute value:" + attr.getnodevalue ());
}
Parsing the child nodes of the book node
NodeList childNodes = Book.getchildnodes ();
Iterate through the childnodes for each node name and value
System.out.println ("the" + (i + 1) + "The book has:" + childnodes.getlength () + "sub-node");
for (int k = 0; k < childnodes.getlength (); k++) {
if (Childnodes.item (k). Getnodetype () = = Node.element_node) {
Gets the node name of the element type node
System.out.print ("s" + (k + 1) + "node names are:" +childnodes.item (k). Getnodename ());
Gets the value of the element type node
System.out.println ("--node value is:" + Childnodes.item (k). Gettextcontent ());
or use SYSTEM.OUT.PRINTLN ("--node value is:" + Childnodes.item (k). Getfirstchild (). Getnodevalue ());
}
}
System.out.println ("===================== End Traversal" + (i + 1) + "contents of this book =============================");
}
} 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 ();
}
}
}
Parsing XML using DOM method