2

I'm currently having an issue with my xpath expressions in java. I'm trying to get a list of shopNames!

I got the following XML;

<?xml version="1.0" encoding="UTF-8"?>
<w:shops xmlns:w="namespace">
    <w:shop>
        <w:shopID>1</w:shopID>
        <w:shopName>ShopName</w:shopName>
        <w:shopURL>ShopUrl</w:shopURL>
    </w:shop>
    <w:shop>
        <w:shopID>2</w:shopID>
        <w:shopName>ShopNames</w:shopName>
        <w:shopURL>ShopUrl</w:shopURL>
    </w:shop>
</w:shops>

And I'm feeding this in a Document to a function alike this:

List<String> getShops(Document d)
    throws Exception
{
    List<String> shopnames = new ArrayList<String>();

    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression expr = xpath.compile("/descendant::w:shop/descendant::w:shopName");
    NodeList nodes = (NodeList) expr.evaluate(d, XPathConstants.NODESET);

    for(int x=0; x<nodes.getLength(); x++)
    {
        shopnames.add("" + nodes.item(x).getNodeValue());
    }
    return shopnames;
}

However the issue is that it simply returns an empty list, I'm suspecting it to be my xpath expression, but I'm not sure about it.

Anyone see the issue here?

4 Answers 4

4

The root Element is not shop but shops. I think, you have to compile this expression:

xpath.compile("/descendant::w:shops/descendant::w:shop/descendant::w:shopName");

You may have to set a namespace context:

xpath.setNamespaceContext(new NamespaceContext() {

   public String getNamespaceURI(String prefix) {
    if (prefix.equals("w")) return "namespace";
    else return XMLConstants.NULL_NS_URI;
   }

   public String getPrefix(String namespace) {
    if (namespace.equals("namespace")) return "w";
    else return null;
   }

   public Iterator getPrefixes(String namespace) {return null;}

});

and parse so that the document is aware of namespaces

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);  // <----
DocumentBuilder db = dbf.newDocumentBuilder();
Document xmlDom = db.parse("./shops.xml");
Sign up to request clarification or add additional context in comments.

11 Comments

Negative, still returns empty.
Where do I set the dbf.setNamespaceAware() ? on the document that builded my document d?
@Skeen - added the full snippet for the parsing part. But maybe its enough to just set the namespace context. Give that a try first and investigate in the parsing process, if it still doesn't work
I'll need few minuts, to get this specific module out of my servlet. - To be able to do some useful testing. - I'll return by then.
@Andreas_D: Also this sentence You may have to set a namespace context should be You have to set a namespace context
|
2

This one also works: //w:shopName/text() is not as "selective", but I think it's more readable. And returns a list of strings, rather than a list of nodes, which might be better or not, depending what you need.

4 Comments

Negative, still returns empty.
it does work, you can try it here futurelab.ch/xmlkurs/xpath.en.html, if it doesn't work, then the problem is not the xpath, but the libraries / code you're using.
The libary is the javax standard one.
Your expression doesn't return strings but a node set of text nodes. Selecting text nodes is not the same as the more semantic selection of elements because mixed content.
2

Don't you need to set a NamespaceContext on your XPath instance ? I think you have to so your 'w' ns is recognized.

2 Comments

Right, seems reasonable :o, now how do I do this?
Well, I'm afraid you have to implement the NamespaceContext yourself. There is no implementation in the jdk.
1

You don't have to specificate nscontext, your XPath expr will be little longer but will say everything alone:

/*[namespace-uri()='namespace'  and local-name()='shops']/*[namespace-uri()='namespace'  and local-name()='shop']/*[namespace-uri()='namespace'  and local-name()='shopName']

so in Java:

        XPathFactory factory = XPathFactory.newInstance();
        XPath xp = factory.newXPath();
        String xpath = 
        "/*[namespace-uri()='namespace'  and local-name()='shops']/*[namespace-uri()='namespace'  and local-name()='shop']/*[namespace-uri()='namespace'  and local-name()='shopName']";
        XPathExpression expr = xp.compile(xpath);
        NodeList nlist = (NodeList) expr.evaluate(e, XPathConstants.NODESET);
        ArrayList<String> shopNamesList = new ArrayList<String>();
        for (int i = 0; i < nlist.getLength(); i++) {
            shopNamesList.add(((Element) nlist.item(i)).getNodeValue());
        }

This should work. Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.