Implementing code for parsing XML files using DOM in Java

Source: Internet
Author: User
The code is as follows Copy Code
Import Java.io.File;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;

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;


public class Demo {

public static void Main (string[] args) throws Exception {
Create a parser factory instance and generate a parser
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
To create a document object that needs to be resolved
File F = new file ("books.xml");
Parses the document and returns a Documents object, at which time the XML document is loaded into memory
Well, let the parsing come more violently, and the rest is to get the data.
Document doc = Builder.parse (f);

Get Document root element
Why do you ask me? Because the Document object itself is a tree structure, here is the root
Of course, you can also directly find the element collection, omit this step
Element root = Doc.getdocumentelement ();

The root node is found above, where we begin to get the collection of elements under the root node
NodeList list = Root.getelementsbytagname ("book");

for (int i = 0; i < list.getlength (); i++) {
Locate the node in the collection by using the item () method and transition down to the element object
Element n = (Element) list.item (i);
Gets the property map in the object, extracts and prints with the For loop
NamedNodeMap node = n.getattributes ();
for (int x = 0; x < node.getlength (); + + +) {
Node nn = node.item (x);
System.out.println (Nn.getnodename () + ":" + nn.getnodevalue ());
}
Print element content, the code is very tangled, almost a fixed format
System.out.println ("title:" +n.getelementsbytagname ("title"). Item (0). Getfirstchild (). Getnodevalue ());
System.out.println ("Author:" + n.getelementsbytagname ("author"). Item (0). Getfirstchild (). Getnodevalue ());
System.out.println ();
}
}

}

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.