Jump to Real's How-to Main page

Parse with XPath

[J2SE 1.5]

Consider the following XML data file (howto.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>
To list the topics :
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
    */
  }
}
Select the topic Powerbuilder then list the urls :
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
    */
  }
}

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2007
[ home ]