Xpath

来源:互联网 发布:代码编写软件 编辑:程序博客网 时间:2024/04/30 19:16
 
  1.    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  2.     domFactory.setNamespaceAware(true); // never forget this!
  3.     DocumentBuilder builder = domFactory.newDocumentBuilder();
  4.     Document doc = builder.parse("..//ehr//src//com//ehr//test//xpath.xml");
  5.     XPathFactory factory = XPathFactory.newInstance();
  6.     XPath xpath = factory.newXPath();
  7.     //编译XPath表达式供以后计算使用。
  8.     XPathExpression expr = xpath.compile("//@id");
  9.     //计算指定上下文中的 XPath 表达式并返回指定类型的结果。
  10.     Object result = expr.evaluate(doc, XPathConstants.NODESET);
  11.     NodeList nodes = (NodeList) result;
  12.     for (int i = 0; i < nodes.getLength(); i++) {
  13.         System.out.println(nodes.item(i).getNodeValue()); 
  14.     }
原创粉丝点击