How to use XPath to get list of nodes from XML DOM?

Discussion in 'Java' started by Peter Rilling, Mar 26, 2005.

  1. I can see how to get a list of nodes based on the name of the tag using the
    method Document.getElementsByTagName. But is there anyway to get a list
    based on an arbitrary XPath expression? For instance, if I wanted a list of
    Nodes where an attribute was a particular value, how would I do that. In
    ..NET, the XmlDocument class has available the SelectNodes method. Is there
    anything comparable in Java?
    Peter Rilling, Mar 26, 2005
    #1
    1. Advertising

  2. Peter Rilling wrote:

    > I can see how to get a list of nodes based on the name of the tag using the
    > method Document.getElementsByTagName. But is there anyway to get a list
    > based on an arbitrary XPath expression? For instance, if I wanted a list of
    > Nodes where an attribute was a particular value, how would I do that. In
    > .NET, the XmlDocument class has available the SelectNodes method. Is there
    > anything comparable in Java?


    Java 1.5 (alias Java 1.5) has built-in XPath support on DOM nodes, see
    the package javax.xml.xpath.
    For instance with the XML being

    <?xml version="1.0" encoding="UTF-8"?>
    <persons>
    <person prename="Lance" surname="Armstrong" />
    <person prename="Lance" surname="Legstrong" />
    </persons>

    the example Java program

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;

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

    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Element;

    public class Test2005032601 {
    public static void main (String[] args) {
    try {
    DocumentBuilderFactory docBuilderFactory =
    DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document xmlDocument = docBuilder.parse(args[0]);

    XPath xpathEvaluator = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xpathEvaluator.evaluate(
    args[1],
    xmlDocument,
    XPathConstants.NODESET
    );
    for (int i = 0; i < nodeList.getLength(); i++) {
    Element element = (Element) nodeList.item(i);
    System.out.println(element.getAttribute("surname"));
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    when called as

    java Test2005032601 test2005032602.xml "//*[@prename = 'Lance']"

    outputs

    Armstrong
    Legstrong

    If you do not have Java 1.5 then with earlier versions you have to rely
    on external packages to provide XPath evaluation, check out whether the
    DOM and/or XSLT package you are using also provides an XPath API.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. O.B.
    Replies:
    1
    Views:
    1,348
  2. gavnosis
    Replies:
    0
    Views:
    500
    gavnosis
    Aug 2, 2003
  3. th3dude
    Replies:
    0
    Views:
    424
    th3dude
    Jan 3, 2007
  4. Water Cooler v2
    Replies:
    0
    Views:
    410
    Water Cooler v2
    Nov 2, 2007
  5. Alan
    Replies:
    6
    Views:
    1,574
Loading...

Share This Page