Xpath use NodeIterator with java

961 views Asked by At

How I use org.w3c.dom.traversal.NodeIterator for iterate Nodes in java using Xpath

I have the follos code but doesn't work.

    import org.w3c.dom.traversal.NodeIterator;

    NodeIterator products = XPathAPI.selectNodeIterator(document, "/ONIXMessage/Product");
    while(products.hasNext()) {
        Node element = products.nextNode(); 
        .......
    }

I don't know what I have to put in the while condition

1

There are 1 answers

0
Martin Honnen On

A W3C DOM NodeIterator should be used as follows:

Node node;
while ((node = products.nextNode()) != null)
{
  ...
}

If you still have problems then show us a sample of the XML input. Usually namespaces are the reason that XPath expressions don't select what users want them to select.