Parse with XPath : XPath « XML « Java






Parse with XPath

    

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
  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());
    }

  }
}

   
    
    
    
  








Related examples in the same category

1.Create an XML document and search by XPath
2.Use XPath to select node
3.Shallow print of node list
4.Deep print of node list
5.XML and XPath utilities
6.Get the String data associated with the XPath selection supplied
7.This program evaluates XPath expressions