码迷,mamicode.com
首页 > 其他好文 > 详细

xpath 学习

时间:2014-12-19 15:37:07      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

[java] view plaincopy
import 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 学习

标签:

原文地址:http://www.cnblogs.com/qiuwei92/p/4173948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!