欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Parse with XPath,parsewithxpath,java使用xpath解

来源: javaer 分享于  点击 20862 次 点评:84

Parse with XPath,parsewithxpath,java使用xpath解


java使用xpath解析xml

xml文件如下:

<?xml version="1.0"?><howto>  <topic name="Java">      <url>http://www.rgagnon/javahowto.htm</url>  </topic>  <topic name="PowerBuilder">      <url>http://www.rgagnon/pbhowto.htm</url>      <url>http://www.rgagnon/pbhowtonew.htm</url>  </topic>  <topic name="Javascript">        <url>http://www.rgagnon/jshowto.htm</url>  </topic>  <topic name="VBScript">       <url>http://www.rgagnon/vbshowto.htm</url>  </topic></howto>

要列出所有的topic节点:

import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.xpath.*;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;public class SimpleXPath {  public static void main(String[] args) throws Exception {    XPath xpath = XPathFactory.newInstance().newXPath();    String xpathExpression = "/howto/topic/@name";    InputSource inputSource = new InputSource("howto.xml");    NodeList nodes = (NodeList) xpath.evaluate       (xpathExpression, inputSource, XPathConstants.NODESET);    int j = nodes.getLength();    for (int i = 0; i < j; i++) {        System.out.println(nodes.item(i).getTextContent());    }    /*    output :       Java       PowerBuilder       Javascript       VBScript    */  }}

找到名字为PowerBuilder的节点,然后输出其url

import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.xpath.*;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;public class SimpleXPath2 {  public static void main(String[] args) throws Exception {    XPath xpath = XPathFactory.newInstance().newXPath();    String topicExpression = "/howto/topic[@name='PowerBuilder']";    InputSource inputSource = new InputSource("howto.xml");    // get nodes with the topic PowerBuilder    NodeList nodes = (NodeList) xpath.evaluate       (topicExpression, inputSource, XPathConstants.NODESET);    // output the text content of this node and its descendants.    //   (includes empty LF because of empty comment (#text))    System.out.println(nodes.item(0).getTextContent());    /*    output :          http://www.rgagnon/pbhowto.htm          http://www.rgagnon/pbhowtonew.htm    */    // display only the "url" nodes for PowerBuidler    NodeList urls = nodes.item(0).getChildNodes();    int j = urls.getLength();    for (int i = 0; i < j ; i++) {        if (urls.item(i).getNodeName().equals("url")) {            System.out.println("url :" + urls.item(i).getTextContent());        }    }    /*    output :       url :http://www.rgagnon/pbhowto.htm       url :http://www.rgagnon/pbhowtonew.htm    */  }}
相关栏目:

用户点评