Java XPath 解析器 - 查詢 XML 文件



Java XPath 是一個 Java API,它幫助我們使用 XPath 表示式查詢 XML 文件。使用 XPath 表示式,可以實現元素的隨機訪問,因此我們可以從 XML 文件中選擇特定的節點。這使得文件查詢更加靈活。

在本章中,我們使用帶有謂詞的 XPath 表示式,例如 '/cars/carname/@company'、'/class/student[@rollno = '493']',並透過各種詳細示例來查詢 XML 文件。

使用 Java XPath 解析器查詢 XML

以下是使用 Java XPath 解析器查詢文件時使用的步驟:

  • 步驟 1:建立 DocumentBuilder
  • 步驟 2:讀取 XML
  • 步驟 3:從檔案或流建立文件
  • 步驟 4:構建 XPath
  • 步驟 5:準備和評估 XPath 表示式
  • 步驟 6:迭代 NodeList
  • 步驟 7:查詢元素

請參考本節的 解析 XML 文件 一章瞭解前六個步驟。

步驟 7:查詢元素

使用 XPath 查詢 XML 文件與解析 XML 文件的步驟相同,但唯一的區別在於我們準備 XPath 表示式的方式。在查詢時,我們會查詢特定屬性或元素。

按元素名稱查詢

我們可以根據元素名稱在 XPath 表示式中指定元素名稱來查詢 XML 文件。XPath 表示式 'root_element/element_name' 檢索根元素內所有具有指定名稱的節點。

示例

以下 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();
      }  
   }
}

輸出

輸出視窗顯示已找到 Bentley 2 汽車。

Bentley 2 car is found

按屬性查詢

要查詢元素內的屬性,我們使用 XPath 表示式 '/root_element/element_name/@attr_name'。此表示式獲取根元素內指定元素的所有具有指定名稱的屬性。所有這些屬性都以 NodeList 中節點的形式存在。

示例 1

我們使用與上一示例中相同的 cars.xml 檔案來計算 Bentley 汽車的總數。我們使用了表示式 '/cars/carname/@company' 獲取所有 company 屬性節點,並透過檢查它是否為 'Bentley' 來遞增計數變數。

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();
      }  
   }
}

輸出

輸出視窗顯示在 XML 文件中找到的 Bentley 汽車數量。

Number of Bentley cars found: 3

示例 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 的學生的資訊。

輸出

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