Java Parsing XML DOM mode preparation
Create a Documentbuilderfactory object;
Create a Documentbuilder object;
Loading XML through the parse method of the Documentbuilder object
Parsing the XML file's attribute name and attribute value parsing the node name and node values of the XML file is commonly used in the following ways:
getElementsByTagName ();
GetLength ();
Item ();
Getnodename ();
Getnodevalue ();
Getnodetype ();
*getattributes (); Node
*getattribute ("name"); Element
Getchildnodes ();
Getfirstchild ();
Common Types of nodes:
Directly below the code
PackagePers.zww.xml;Importjava.io.IOException;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;Importorg.w3c.dom.Document;ImportOrg.w3c.dom.NamedNodeMap;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classDom { Public Static voidMain (string[] args) {//Create a Documentbuilderfactory objectDocumentbuilderfactory DBF =documentbuilderfactory.newinstance (); //Create a Documentbuilder objectDocumentbuilder DB; Try{db=Dbf.newdocumentbuilder (); //loading XML through the parse method of the Documentbuilder objectDocument document = Db.parse ("books.xml"); //Get book node collectionNodeList Booklist = document.getElementsByTagName ("book"); //get the length by Noodlist's GetLength () methodSystem.out.println (Booklist.getlength ()); //Traverse Book Node for(inti = 0; I < booklist.getlength (); i++) {System.out.println ("======================="); //Item (i) Method gets nodeNode book =(Node) Booklist.item (i); //gets all the property collections of the book NodeNamedNodeMap attrs =book.getattributes (); //traversing the properties of a bookSystem.out.println ("No." + (i + 1) + "book Total" +attrs.getlength ()+ "Properties"); for(intj = 0; J < Attrs.getlength (); J + +) { /** Node Type * Element 1 * attr 2 * Text 3*/ /** If there is only one attribute, * Convert the book node to the type conversion * with element getattribute ("I D ") method*/ //Item (i) method gets the propertyNode attr =Attrs.item (j); //Get Property nameSystem.out.print ("Property name:" +attr.getnodename ()); //Get Property valueSYSTEM.OUT.PRINTLN ("&& attribute value:" +Attr.getnodevalue ()); //parse the child nodes of bookNodeList childnodes=book.getchildnodes (); //get node name/value for each nodeSYSTEM.OUT.PRINTLN ("Total" +childnodes.getlength () + "child nodes"); for(intk = 0; K < Childnodes.getlength (); k++) { //node that distinguishes between the text type and the element type if(Childnodes.item (k). Getnodetype () = =Node.element_node) { //Gets the node name of the element type nodeSystem.out.print ("Node name" +Childnodes.item (k). Getnodename ()); //Gets the node value of the element type node//Law One:System.out.println ("&& node value" +Childnodes.item (k). Getfirstchild (). Getnodevalue ()); //Law II://all values for child nodes//System.out.println ("&& node Value" +childnodes.item (k). Gettextcontent ()); /** Different points: * If you add another node under the name node, such as */ } } } } } Catch(Parserconfigurationexception | saxexception |IOException E) {E.printstacktrace (); } }}
View Code
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Bookstore>3 < BookID= "1">4 <name><h>Xiumu</h>Java Programming Ideas</name>5 <Anthor>****</Anthor>6 < Year>2000</ Year>7 </ Book>8 < BookID= "2">9 <name>Crazy Java Series</name>Ten <Anthor>Li Gang</Anthor> One < Price>89</ Price> A </ Book> - </Bookstore>
View CodePoints
When you have determined that there is only one property for a node, you can cast the node to element and call its getattribute ("name") method;
GetLength () method The number of child nodes, will be blank and newline also forget to go in;
Resources
MU Lesson net Jessicajiang Teacher Video Tutorial
Java parsing the DOM way of XML