首页 > 代码库 > xpath 学习
xpath 学习
[java] view plaincopyimport java.io.IOException; import javax.xml.parsers.*; import javax.xml.xpath.*; import org.w3c.dom.*; import org.xml.sax.SAXException; public class XpathTest { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("C:/Users/Administrator/Desktop/test.xml"); System.out.println(doc.getChildNodes().getLength()); XPathFactory xFactory = XPathFactory.newInstance(); XPath xpath = xFactory.newXPath(); XPathExpression expr = xpath .compile("//name/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } } }
xpath 学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。