原文: https://howtodoinjava.com/xml/evaluate-xpath-on-xml-string/

Java 示例求值字符串上的 xpath 并返回字符串本身的结果 XML。

1. XPath 示例 – 在 xml 字符串上求值 xpath

  1. 创建包含StringReader并引用 XML 字符串的org.xml.sax.InputSource
  2. XPathFactory创建XPath
  3. 使用xpath.evaluate('expression', inputSource)获取结果 HTML。
  1. package com.howtodoinjava.demo;
  2. import java.io.StringReader;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import javax.xml.xpath.XPath;
  6. import javax.xml.xpath.XPathConstants;
  7. import javax.xml.xpath.XPathExpression;
  8. import javax.xml.xpath.XPathExpressionException;
  9. import javax.xml.xpath.XPathFactory;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.NodeList;
  12. import org.xml.sax.InputSource;
  13. public class XPathExample
  14. {
  15. public static void main(String[] args) throws Exception
  16. {
  17. String xml = "<employees>"
  18. + "<employee id=\"1\">"
  19. + "<firstName>Lokesh</firstName>"
  20. + "<lastName>Gupta</lastName>"
  21. + "<department><id>101</id><name>IT</name></department>"
  22. + "</employee>"
  23. + "</employees>";
  24. InputSource inputXML = new InputSource( new StringReader( xml ) );
  25. XPath xPath = XPathFactory.newInstance().newXPath();
  26. String result = xPath.evaluate("/employees/employee/firstName", inputXML);
  27. System.out.println(result);
  28. }
  29. }

程序输出:

  1. Lokesh

2. XPath 示例 – 在 xml 文件上求值 xpath

  1. package com.howtodoinjava.demo;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import javax.xml.parsers.DocumentBuilderFactory;
  4. import javax.xml.xpath.XPath;
  5. import javax.xml.xpath.XPathConstants;
  6. import javax.xml.xpath.XPathExpression;
  7. import javax.xml.xpath.XPathExpressionException;
  8. import javax.xml.xpath.XPathFactory;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.NodeList;
  11. public class XPathExample
  12. {
  13. public static void main(String[] args) throws Exception
  14. {
  15. String xmlFile = "employees.xml";
  16. //Get DOM
  17. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  18. DocumentBuilder db = dbf.newDocumentBuilder();
  19. Document xml = db.parse(xmlFile);
  20. //Get XPath
  21. XPathFactory xpf = XPathFactory.newInstance();
  22. XPath xpath = xpf.newXPath();
  23. //Get first match
  24. String name = (String) xpath.evaluate("/employees/employee/firstName", xml, XPathConstants.STRING);
  25. System.out.println(name); //Lokesh
  26. //Get all matches
  27. NodeList nodes = (NodeList) xpath.evaluate("/employees/employee/@id", xml, XPathConstants.NODESET);
  28. for (int i = 0; i < nodes.getLength(); i++) {
  29. System.out.println(nodes.item(i).getNodeValue()); //1 2
  30. }
  31. }
  32. }

程序输出:

  1. Lokesh
  2. 1
  3. 2

输入的 xml 文件为:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <employees>
  3. <employee id="1">
  4. <firstName>Lokesh</firstName>
  5. <lastName>Gupta</lastName>
  6. <department>
  7. <id>101</id>
  8. <name>IT</name>
  9. </department>
  10. </employee>
  11. <employee id="2">
  12. <firstName>Brian</firstName>
  13. <lastName>Schultz</lastName>
  14. <department>
  15. <id>102</id>
  16. <name>HR</name>
  17. </department>
  18. </employee>
  19. </employees>

在上面的 Xpath 教程中,我们学习了使用示例求值字符串上的 xpath 的方法。

学习愉快!

阅读更多:

文件中的 Java xpath 示例

Java xpath 教程

如何在 Java 中使用 xpath 获取 xml 中的属性值