xpath语法参考:XPath 语法 | 菜鸟教程
@Test
public void testXml() throws XPathExpressionException, ParserConfigurationException, IOException, SAXException {
String xml = "<users>\n" +
" <user name=\"zs\" age=\"18\">\n" +
" <height>180</height>\n" +
" <weight>140</weight>\n" +
" <hobby>basketball1</hobby>\n" +
" </user>\n" +
" <user name=\"ls\" age=\"17\">\n" +
" <height>160</height>\n" +
" <weight>160</weight>\n" +
" <hobby>basketball2</hobby>\n" +
" </user>\n" +
"</users>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile("//user/hobby[contains(text(),'basketball3')]");
boolean flag = (Boolean)expr.evaluate(doc, XPathConstants.BOOLEAN);
Assert.assertEquals(flag,true);
System.out.println(flag);
}
@Test
public void testXml1() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
String xml = "<users>\n" +
" <user name=\"zs\" age=\"18\">\n" +
" <height>180</height>\n" +
" <weight>140</weight>\n" +
" <hobby>basketball</hobby>\n" +
" </user>\n" +
" <user name=\"ls\" age=\"17\">\n" +
" <height>160</height>\n" +
" <weight>160</weight>\n" +
" <hobby>basketball</hobby>\n" +
" </user>\n" +
"</users>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
System.out.println(doc.getChildNodes().getLength());
XPathFactory xFactory = XPathFactory.newInstance();
XPath xpath = xFactory.newXPath();
XPathExpression expr = xpath
.compile("//user[@name='zs']/height");
String height = (String)expr.evaluate(doc, XPathConstants.STRING);
System.out.println(height);
}
@Test
public void testXml2() throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
String xml = "<users>\n" +
" <user name=\"zs\" age=\"18\">\n" +
" <height>180</height>\n" +
" <weight>140</weight>\n" +
" <hobby>basketball</hobby>\n" +
" </user>\n" +
" <user name=\"ls\" age=\"17\">\n" +
" <height>160</height>\n" +
" <weight>160</weight>\n" +
" <hobby>basketball</hobby>\n" +
" </user>\n" +
"</users>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
System.out.println(doc.getChildNodes().getLength());
XPathFactory xFactory = XPathFactory.newInstance();
XPath xpath = xFactory.newXPath();
XPathExpression expr = xpath
.compile("//user[@name='zs']");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
System.out.println(nodes.getLength() );
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
System.out.println(node.getNodeValue()==null? node.getTextContent():node.getNodeValue());
}
}