Java Xml 简明教程

Java XPath Parser - Query XML Document

Java XPath 是一种 Java API,它帮助我们使用 XPath 表达式查询 XML 文档。使用 XPath 表达式可以随机访问元素,因此,我们可以从 XML 文档中选定特定节点。这使得查询文档更加灵活。

在本章中,我们使用条件谓词(如 '/cars/carname/@company'、'/class/student[@rollno = '493']')的 XPath 表达式查询了 XML 文档,并提供了多种详细的示例。

Query XML Using Java XPath Parser

以下是在使用 XPath 查询器查询文档时使用的步骤 −

  1. 步骤 1: 创建 DocumentBuilder

  2. 步骤 2: 读取 XML

  3. 步骤 3: 从文件或流创建 Document

  4. *Step 4: *Building XPath

  5. 步骤 5: 准备和评估 XPath 表达式

  6. 步骤 6: 遍历 NodeList

  7. *Step 7: *Querying Elements

请参阅本部分的 Parse XML Document 章节了解前六个步骤。

Step 7: Querying Elements

使用 XPath 查询 XML 文档的步骤与解析 XML 文档相同,唯一区别在于准备 XPath 表达式的方式。在进行查询时,我们要查询某个特定属性或元素。

Querying by Element Names

我们可以通过在 XPath 表达式中指定元素名,根据它们的元素名查询 XML 文档。XPath 表达式 'root_element/element_name' 检索根元素中具有指定名称的所有节点。

Example

以下 cars.xml 文件包含有关七辆汽车的信息,根元素 'cars' 中的元素名为 'carname'。我们要查询这个 XML 文件以检查是否存在“Bentley 2”汽车。

<?xml version = "1.0"?>
<cars>
      <carname company="Ferarri" >Ferarri 101</carname>
      <carname company="Lamborgini">Lamborgini 001</carname>
      <carname company="Lamborgini">Lamborgini 002</carname>
      <carname company="Lamborgini">Lamborgini 003</carname>
      <carname company="Bentley">Bentley 1</carname>
      <carname company="Bentley">Bentley 2</carname>
      <carname company="Bentley">Bentley 3</carname>
</cars>

以下 QueryByElements.java 程序读取上面的 cars.xml 文件并构建一个文档。使用 XPath 表达式 '/cars/carname' ,我们获得 carname 元素作为 nodeList 中的节点。我们遍历 NodeList 以找到 Bentley 2 汽车。

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class QueryByElements {
   public static void main(String[] args) {
      try {

         //Creating DocumentBuilder
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

         //Reading the XML
         File inputFile = new File("cars.xml");

         //Creating Document from file or Stream
         Document doc = dBuilder.parse(inputFile);

         //Building XPath
         XPath xPath =  XPathFactory.newInstance().newXPath();

         //Preparing and Evaluating XPath expression
         String expression = "/cars/carname";
         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
            doc, XPathConstants.NODESET);

         //Iterating over NodeList
         boolean found = false;
         for (int i = 0; i < nodeList.getLength(); i++) {
            Node nNode = nodeList.item(i);
            //Querying the Elements
            if(nNode.getTextContent().equals("Bentley 2"))
               found=true;
         }
         if(found)
        	System.out.println("Bentley 2 car is found");
         else
        	 System.out.println("Bentley 2 car is not found");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

输出窗口显示找到 Bentley 2 汽车。

Bentley 2 car is found

Querying by Attributes

要查询元素内部的属性,我们使用 XPath 表达式 '/root_element/element_name/@attr_name' 。此表达式获取根元素中具有指定元素的指定名称的所有属性。所有这些属性都以节点的形式存在于 NodeList 中。

Example 1

我们使用在前面的示例中使用的相同 cars.xml 文件来计算 Bentley 汽车的总数。我们使用了表达式 '/cars/carname/@company' 来获取所有 company 属性节点,并通过将其与 'Bentley' 进行检查来增加 count 变量。

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class QueryByAttributes {
   public static void main(String[] args) {
      try {

         //Creating a DocumentBuilder
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

         //Reading the XML
         File inputFile = new File("cars.xml");

         //Creating Document from file or Stream
         Document doc = dBuilder.parse(inputFile);

         //Building XPath
         XPath xPath =  XPathFactory.newInstance().newXPath();

         //Preparing and Evaluating XPath expression
         String expression = "/cars/carname/@company";
         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
            doc, XPathConstants.NODESET);

         //Iterating over NodeList
         int count=0;
         for (int i = 0; i < nodeList.getLength(); i++) {
            Node nNode = nodeList.item(i);
            //Querying the Elements
            if(nNode.getNodeValue().equals("Bentley"))
               count++;
         }
         System.out.println("Number of Bentley cars found: " + count);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

输出窗口显示在 XML 文档中找到的 Bentley 汽车数量。

Number of Bentley cars found: 3

Example 2

我们需要查询以下 studentData.xml 才能仅获取与学号为 493 的学生相关的信息。

<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>

   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>

   <student rollno = "593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>

在以下 QueryXPathDemo.java 程序中,我们解析了上面的 studentData.xml 文件并生成了一个文档。XPath 表达式 '/class/student[@rollno = '493']' 用于获取学号为 493 的该学生元素。

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class QueryXPathDemo {

   public static void main(String[] args) {

      try {

         //Creating a DocumentBuilder
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

         //Reading the XML
         File inputFile = new File("studentData.xml");

         //Creating Document from file or Stream
         Document doc = dBuilder.parse(inputFile);

         //Building XPath
         XPath xPath =  XPathFactory.newInstance().newXPath();

         //Preparing and Evaluating XPath expression
         String expression = "/class/student[@rollno = '493']";
         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
            doc, XPathConstants.NODESET);

         //Iterating over NodeList
         for (int i = 0; i < nodeList.getLength(); i++) {
            Node nNode = nodeList.item(i);
            System.out.println("\nCurrent Element :" + nNode.getNodeName());
            //Getting student info with roll number 493
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               System.out.println("Student roll no : "
                  + eElement.getAttribute("rollno"));
               System.out.println("First Name : "
                  + eElement
                  .getElementsByTagName("firstname")
                  .item(0)
                  .getTextContent());
               System.out.println("Last Name : "
                  + eElement
                  .getElementsByTagName("lastname")
                  .item(0)
                  .getTextContent());
               System.out.println("Nick Name : "
                  + eElement
                  .getElementsByTagName("nickname")
                  .item(0)
                  .getTextContent());
               System.out.println("Marks : "
                  + eElement
                  .getElementsByTagName("marks")
                  .item(0)
                  .getTextContent());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

输出窗口显示学号为 493 的学生的信息。

Output

Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95