首页 > 其他 > 详细

xpath 学习

时间:2014-12-19 15:34:22      阅读:159      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!