Java parsing XML sample sharing _java using XPath

Source: Internet
Author: User
Tags xpath

XPath is the XML Path language (XML Path Language), which is a language used to determine the location of a part of an XML document. XPath is based on an XML tree structure that provides the ability to find nodes in a data structure tree. At first the original idea of XPath was to use it as a generic syntax model between XPointer and XSL. But XPath was quickly used by developers as a small query language.

Xpathtest.java

Copy Code code as follows:

Package com.hongyuan.test;

Import Java.io.File;
Import java.io.IOException;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.xpath.XPath;
Import javax.xml.xpath.XPathConstants;
Import javax.xml.xpath.XPathExpressionException;
Import Javax.xml.xpath.XPathFactory;

Import org.w3c.dom.Document;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;

public class Xpathtest {

public static void Main (string[] args) throws Parserconfigurationexception,
Saxexception, IOException, xpathexpressionexception {

Parsing a file, generating a Document object
Documentbuilder builder = documentbuilderfactory.newinstance ()
. Newdocumentbuilder ();
Document document = Builder.parse (new File ("Bookstore.xml"));

To generate an XPath object
XPath XPath = xpathfactory.newinstance (). Newxpath ();

Get node value
String webtitle = (string) xpath.evaluate (
"/bookstore/book[@category = ' WEB ']/title/text ()", document,
xpathconstants.string);
System.out.println (Webtitle);

System.out.println ("===========================================================");

Get Node Property value
String Webtitlelang = (string) xpath.evaluate (
"/bookstore/book[@category = ' WEB ']/title/@lang", document,
xpathconstants.string);
System.out.println (Webtitlelang);

System.out.println ("===========================================================");

Get Node Object
Node Bookweb = (node) xpath.evaluate (
"/bookstore/book[@category = ' WEB ']", document,
Xpathconstants.node);
System.out.println (Bookweb.getnodename ());

System.out.println ("===========================================================");

Get node Collection
NodeList books = (nodelist) xpath.evaluate ("/bookstore/book", document,
Xpathconstants.nodeset);
for (int i = 0; i < books.getlength (); i++) {
Node book = Books.item (i);
System.out.println (Xpath.evaluate ("@category", book,
xpathconstants.string));
}

System.out.println ("===========================================================");
}

}

Bookstore.xml

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<bookstore>
<book category= "Cooking" >
<title lang= "en" >everyday italian</title>
<author>giada De laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category= "CHILDREN" >
<title lang= "en" >harry potter</title>
<author>j K. rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category= "WEB" >
<title lang= "en" >learning xml</title>
<author>erik T. ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>


Run effect

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.