Take the 2-minute tour ×

I want to read Sitemap with XPath but it doesn't work. here is my code :

private void evaluate2(String src){
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(src.getBytes()));


        XPathFactory xp_factory = XPathFactory.newInstance();

        XPath xpath = xp_factory.newXPath();

        XPathExpression expr = xpath.compile("//url/loc");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;


    }catch(Exception e){

Before I retrieve the remote source of the sitemap, and it's passed to evaluate2 through the variable src. And the System.out.println(nodes.getLength()); display 0 My xpath query is working because this query work in PHP. Do you see errors in my code ?


share|improve this question
add comment

3 Answers

I think the input has namespace. So you would have to initialize the namespaceContext for the xpath object and change your xpath with prefixes. i.e. //usr/loc should be //ns:url/ns:loc and then add the namespace prefix binding in the namespace object.

You can find an NamespaceContext implementation available with apache common. http://ws.apache.org/commons/util/apidocs/index.html ws-commons-utils

NamespaceContextImpl namespaceContextObj = new NamespaceContextImpl();
    nsContext.startPrefixMapping("ns", "http://sitename/xx");

XPathExpression expr = xpath.compile("//ns:url/ns:loc");

In case you don't know what namespaces that are comming, you can get them from the document it self, but I doubt it ll be of much use. There are few how-tos here http://www.ibm.com/developerworks/xml/library/x-nmspccontext/index.html

share|improve this answer
But the namespace coulb be different according to sitemap version no ? –  guillaume Dec 20 '12 at 19:37
updated the response. –  Vamsi Mohan Jayanti Dec 20 '12 at 21:33
add comment

You parse the sitemap with a namespace-aware parser (that's what factory.setNamespaceAware(true) does), but then attempt to access it using an XPath that does not usea namespace resolver (or reference any namespaces).

The simplest solution is to configure the parser as not namespace aware. As long as you're just parsing a self-contained sitemap, that shouldn't be a problem.

One more problem in your code is that you pass the sitemap contents as a String, then convert that String using the platform default encoding. This will work as long as your platform-default encoding matches that of the actual bytes that you retrieved from the server (assuming that you also created the string using the platform-default encoding). If it doesn't, you're likely to get a conversion error.

share|improve this answer
add comment

I can't see any errors in your code so I gues the problem is the source. Are you sure that the source file contains this element?

Maybe you could try to use this code to parse the String in an Document

builder.parse(new InputSource(new StringReader(xml)));
share|improve this answer
If I do this and I put a System.out.println(doc.toString()); I've the message [#document: null] Is it normal ? –  guillaume Dec 20 '12 at 18:45
no thats not normal. It says that you Document object is empty. SO try it with the tip of pasifal(setNameSpaceAware(false)) –  maseas Dec 20 '12 at 18:57
add comment

Your Answer


