Java中的 XPath 评估器
众所周知,XML 文档用于存储和传输数据。因此,要从 XML 访问数据,我们需要可以访问每个节点和相应属性数据的东西。那么解决方案是XPath。 XPath 可用于遍历 XML 文档、选择节点/元素和属性数据。它是一项 W3C 推荐标准,是一种访问 XML 文档不同部分的灵活方式。编写 XPath 类似于在您的计算机系统中编写路径表达式以遍历特定位置,例如 (C:/School/Homework/assignment.docx)。
考虑以下 XML 文档
XML
Divyank Singh Sikarwar
18
Agra
Aniket Chauhan
20
Shahjahanpur
Simran Agarwal
23
Buland Shar
Abhay Chauhan
17
Shahjahanpur
Himanshu Bhatia
25
Indore
Anuj Modi
22
Ahemdabad
Manoj Yadav
23
Kota
Java
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XPathDemo {
public static void main(String[] args) throws Exception
{
File xmlFile = new File("student.xml");
// Get DOM
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document xml = db.parse(xmlFile);
xml.getDocumentElement().normalize();
// Get XPath
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
// Find 2nd Student's name
String name = (String)xpath.evaluate(
"/students/student[2]/name", xml,
XPathConstants.STRING);
System.out.println("2nd Student Name: " + name);
// find specific students name whose branch is IT
NodeList nodes = (NodeList)xpath.evaluate(
"/students/student[@branch = \"IT\"]/name", xml,
XPathConstants.NODESET);
System.out.println("\nStudents with branch IT:");
printNodes(nodes);
// find specific students
// name whose age is less
// than equal to 20
nodes = (NodeList)xpath.evaluate(
"/students/student[age <= 20]/name", xml,
XPathConstants.NODESET);
System.out.println(
"\nStudents of age less than equal to 20:");
printNodes(nodes);
// First 4 students from XML document
nodes = (NodeList)xpath.evaluate(
"/students/student[position() < 5]/name", xml,
XPathConstants.NODESET);
System.out.println("\nFirst Four Students: ");
printNodes(nodes);
}
// prints nodes
public static void printNodes(NodeList nodes)
{
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(
(i + 1) + ". "
+ nodes.item(i).getTextContent());
}
}
}
用于访问 XML 文档不同部分的 XPath 符号:
Symbol | Description | Example | Result |
---|---|---|---|
name | Selects all tags from XML having name ‘name’ | /students/student/name | Displays all names |
/ | This represents the root of the document | /students/student/city | Display each student’s city |
// | Selects node irrespective of where it is. | //age | Selects and display all ages |
@ | To access attribute value of XML tags | /students/student/@branch | Display each student’s branch |
[ ] | It is used to select specific nodes | /students/student[2]/name | Displays Aniket Chauhan |
让我们练习 XPath
考虑上面提到的 XML 文档:
选择第二个学生
/students/student[2]/name
选择所有具有分支 IT 的学生
/students/student[@branch = /”IT/”]/name
选择所有年龄小于等于 20 的学生
/students/student[age <= 20]/name
前4名学生
/students/student[position() <= 4]/name
用于评估 XPath 表达式的Java代码
Java
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XPathDemo {
public static void main(String[] args) throws Exception
{
File xmlFile = new File("student.xml");
// Get DOM
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document xml = db.parse(xmlFile);
xml.getDocumentElement().normalize();
// Get XPath
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
// Find 2nd Student's name
String name = (String)xpath.evaluate(
"/students/student[2]/name", xml,
XPathConstants.STRING);
System.out.println("2nd Student Name: " + name);
// find specific students name whose branch is IT
NodeList nodes = (NodeList)xpath.evaluate(
"/students/student[@branch = \"IT\"]/name", xml,
XPathConstants.NODESET);
System.out.println("\nStudents with branch IT:");
printNodes(nodes);
// find specific students
// name whose age is less
// than equal to 20
nodes = (NodeList)xpath.evaluate(
"/students/student[age <= 20]/name", xml,
XPathConstants.NODESET);
System.out.println(
"\nStudents of age less than equal to 20:");
printNodes(nodes);
// First 4 students from XML document
nodes = (NodeList)xpath.evaluate(
"/students/student[position() < 5]/name", xml,
XPathConstants.NODESET);
System.out.println("\nFirst Four Students: ");
printNodes(nodes);
}
// prints nodes
public static void printNodes(NodeList nodes)
{
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(
(i + 1) + ". "
+ nodes.item(i).getTextContent());
}
}
}
输出:
对上述代码中使用的类和方法的说明:
- javax.xml.Parsers.DocumentBuilder类定义了从 XML 文档获取 DOM 实例的 API。
- parse()方法将给定文件的内容解析为 XML 文档并返回一个新的 DOM 对象。
- normalize()方法归一化 给定文件的内容作为 XML 文档。
- javax.xml.xpath.XPathFactory类实例可用于创建包含evaluate()方法的XPath 对象来评估我们编写的xpath 并返回字符串/Node/NodeSet,任何人,根据传递的参数(请参阅里面的evaluate() 方法)代码)。
- position()是一个 XPath函数,它返回当前指定标签的位置。 (在上面的代码中指定的标签是'student')。同样,XPath 提供了一个有用的函数列表,您可以探索它。