Java USES xpath to parse XML sample shares

  • 2020-04-01 03:13:00
  • OfStack

XPath is known as XML Path Language, which is a Language used to locate parts of an XML document. XPath is an xml-based tree structure that provides the ability to find nodes in a data structure tree. XPath was originally conceived as a generic syntax model between XPointer and XSL. But XPath was quickly adopted by developers as a small query language.

XPathTest. Java


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 {
  //Parse the file to generate the document object
  DocumentBuilder builder = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder();
  Document document = builder.parse(new File("bookstore.xml"));
  //Generate XPath objects
  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("===========================================================");
  //Gets the 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 set
  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("===========================================================");
 }
}

XML bookstore.


<?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>


Running effect

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201403/20140323091252.jpg? 20142239143 ">


Related articles: