Parsing an XML file using the DOM in Java:
- Create Documentbuilderfactory object Documentbuilderfactory dbf = Documentbuilderfactory.newinstanc E ();
- Create a Documentbuilder object
- Loading an XML file into the current project through the parser method of the Documentbuilder object
- Gets a collection of all the nodes within the XML file getElementsByTagName () method
- Iterate through each of the nodes
- Get node by item (i) method
- Gets the collection of all properties for the node GetAttributes () method
- Traversing the properties of a node Getnodevalue ()
- Get the properties of a node through the item () method
- Get property names and property values by Getnodename () and Getnodevalue ()
- Get a collection of child nodes by Getchildnodes ()
- Iterate through the child nodes, get the child node property names and attribute values (Note: Distinguish the text type of node, use the IF (Chilnod.item (k). Getnodetype () = = Node.element_node))
To get the book. XML file As an example, the code is as follows:
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) {
Create an Documentbuilderfactory object
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
try {
Create a Documentbuilder object
Documentbuilder db = Dbf.newdocumentbuilder ();
The books is loaded by the parser method of the Documentbuilder object. XML file to current project
Document document = Db.parse ("books.xml");
Gets the collection of all book nodes
NodeList Booklist = document.getElementsByTagName ("book");
System.out.println ("A total of" +booklist.getlength () + "book");
Traverse each book node
for (int i= 0; i<=booklist.getlength (); i++) {
Get the book node through the item (i) method
Node book = Booklist.item (i);
Gets all the property collections of the book Node
NamedNodeMap attrs = Book.getattributes ();
System.out.println ("i+1" + "book in total" +attrs.getlength () + "Properties");
Traversing the properties of a book
for (int j = 0;j<attrs.getlength (); j + +) {
To get the properties of a book node through the Item method
Node attr = Attrs.item (j);
Get Property name
System.out.print ("attribute name:" +attr.getnodename ());
Get property value
System.out.println ("--Attribute value" +attr.getnodevalue ());
NodeList Chilnod = Book.getchildnodes ();
System.out.println ("i+1" + "book in total" +chilnod.getlength () + "sub-node");
for (int k = 0;k<chilnod.getlength (); k++) {
Node chil = Chilnod.item (k);
Distinguish the text type node
if (Chilnod.item (k). Getnodetype () = = Node.element_node) {
System.out.print ("Child node Name:" +chil.getnodename () + ":");
SYSTEM.OUT.PRINTLN ("Child node Value:" +chilnod.item (k). Getfirstchild (). Getnodevalue ());
SYSTEM.OUT.PRINTLN ("Child node Value:" +chilnod.item (k). Gettextcontent ());
}
}
}
}
} 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 files using the DOM in Java