Java Xml 简明教程
Java XPath Parser - Query XML Document
Java XPath 是一种 Java API,它帮助我们使用 XPath 表达式查询 XML 文档。使用 XPath 表达式可以随机访问元素,因此,我们可以从 XML 文档中选定特定节点。这使得查询文档更加灵活。
Java XPath is a Java API that helps us query XML documents using XPath expressions. Using XPath expressions, random access of elements is possible and hence we can select specific nodes from an XML document. This makes querying the documents more flexible.
在本章中,我们使用条件谓词(如 '/cars/carname/@company'、'/class/student[@rollno = '493']')的 XPath 表达式查询了 XML 文档,并提供了多种详细的示例。
In this chapter, we have used XPath expressions with predicates such as '/cars/carname/@company', '/class/student[@rollno = '493']' to query an XML document with various examples in detail.
Query XML Using Java XPath Parser
以下是在使用 XPath 查询器查询文档时使用的步骤 −
Following are the steps used while querying a document using Java XPath Parser −
-
*Step 1: *Creating a DocumentBuilder
-
*Step 2: *Reading the XML
-
*Step 3: *Creating Document from file or Stream
-
*Step 4: *Building XPath
-
*Step 5: *Preparing and Evaluating XPath expression
-
*Step 6: *Iterating over NodeList
-
*Step 7: *Querying Elements
请参阅本部分的 Parse XML Document 章节了解前六个步骤。
Refer Parse XML Document chapter of this section for the first six steps.
Step 7: Querying Elements
使用 XPath 查询 XML 文档的步骤与解析 XML 文档相同,唯一区别在于准备 XPath 表达式的方式。在进行查询时,我们要查询某个特定属性或元素。
Querying the XML document using XPath follows the same steps as parsing the XML document, but the only difference is seen in the way we prepare our XPath expressions. While querying, we query for a particular attribute or element.
Querying by Element Names
我们可以通过在 XPath 表达式中指定元素名,根据它们的元素名查询 XML 文档。XPath 表达式 'root_element/element_name' 检索根元素中具有指定名称的所有节点。
We can query the XML document based on their element names by specifying the element name inside the XPath expression. The XPath expression, 'root_element/element_name' retrieves all the nodes with the specified name inside the root element.
Example
以下 cars.xml 文件包含有关七辆汽车的信息,根元素 'cars' 中的元素名为 'carname'。我们要查询这个 XML 文件以检查是否存在“Bentley 2”汽车。
The following cars.xml file has information about seven cars with the element name as 'carname' inside the root element 'cars'. We are going to query this XML file to check if there is 'Bentley 2’car.
<?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 汽车。
The following QueryByElements.java program reads the above cars.xml file and builds a document. Using the XPath expression '/cars/carname', we get the carname elements as nodes inside a nodeList. We iterate through the NodeList to find out Bentley 2 car.
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();
}
}
}
Querying by Attributes
要查询元素内部的属性,我们使用 XPath 表达式 '/root_element/element_name/@attr_name' 。此表达式获取根元素中具有指定元素的指定名称的所有属性。所有这些属性都以节点的形式存在于 NodeList 中。
To query attributes inside elements, we use the XPath expression '/root_element/element_name/@attr_name'. This expression fetches all the attributes with specified name of the specified element inside the root element. All these attributes are in the form of nodes inside the NodeList.
Example 1
我们使用在前面的示例中使用的相同 cars.xml 文件来计算 Bentley 汽车的总数。我们使用了表达式 '/cars/carname/@company' 来获取所有 company 属性节点,并通过将其与 'Bentley' 进行检查来增加 count 变量。
We use the same cars.xml file we have used in the previous example to count the total number of Bentley cars. We have used the expression, '/cars/carname/@company' to get all the company attribute nodes and by checking it with 'Bentley', we are incrementing the count variable.
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 汽车数量。
The output window displays number of Bentley cars found in the XML document.
Number of Bentley cars found: 3
Example 2
我们需要查询以下 studentData.xml 才能仅获取与学号为 493 的学生相关的信息。
We need to query the following studentData.xml to get only the information related to the student with roll number 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 的该学生元素。
In the following QueryXPathDemo.java program, we have parsed the above studentData.xml file and built a document. The XPath expression, '/class/student[@rollno = '493']' is used to get that student element with roll number 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 的学生的信息。
The output window displays the information of the student with roll number 493.