首页 > 代码库 > 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 学习