parsing XML files using the DOM in Java

Source: Internet
Author: User

Parsing an XML file using the DOM in Java:
 
    1. Create Documentbuilderfactory object Documentbuilderfactory dbf = Documentbuilderfactory.newinstanc E ();
    2. Create a Documentbuilder object
    3. Loading an XML file into the current project through the parser method of the Documentbuilder object
    4. Gets a collection of all the nodes within the XML file getElementsByTagName () method
    5. Iterate through each of the nodes
    6. Get node by item (i) method
    7. Gets the collection of all properties for the node GetAttributes () method
    8. Traversing the properties of a node Getnodevalue ()
    9. Get the properties of a node through the item () method
    10. Get property names and property values by Getnodename () and Getnodevalue ()
    11. Get a collection of child nodes by Getchildnodes ()
    12. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.