1

I am trying to get values from an XML using XPATH. I received the following exception:

    [Fatal Error] books.xml:4:16: The prefix "abc" for element "abc:priority" is not bound.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:///D:/XSL%20TEST%20APP%20BACK%20UP/XMLTestApp/books.xml; lineNumber: 4; columnNumber: 16; The prefix "abc" for element "abc:priority" is not bound.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at xpath.XPathExample.main(XPathExample.java:18)

I am getting this error because my XML is a little bit of different from normal one (please see below):

<?xml version="1.0" encoding="UTF-8"?>
<inventory>
    <Sample>
    <abc:priority>1</abc:priority>  
    <abc:value>2</abc:value>        
    </Sample>
</inventory>

Here is my code (Java) to get values from the above XML:

import java.io.IOException;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;

    public class XPathExample {

      public static void main(String[] args) 
       throws ParserConfigurationException, SAXException, 
              IOException, XPathExpressionException {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("books.xml");

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr 
         = xpath.compile("//Sample/*/text()");////book/Sample[author='Neal Stephenson']/title/text()

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue()); 
        }

      }

    }

If I remove the semicolon, I never get this error.

Is it possible to get content from an XML like mentioned above using XPATH?

2 Answers 2

2

"Is it possible to get content from an XML like mentioned above using Xpath ?" - I don't think so. This XML isn't well-formed.

From the spec (http://www.w3.org/TR/REC-xml-names/#ns-qualnames):

The Prefix provides the namespace prefix part of the qualified name, and MUST be associated with a namespace URI reference in a namespace declaration. [Definition: The LocalPart provides the local part of the qualified name.]

In order to do anything with it, I think you'll have to add a namespace declaration.

Example

<inventory xmlns:abc="x">
    <Sample>
        <abc:priority>1</abc:priority>  
        <abc:value>2</abc:value>        
    </Sample>
</inventory>
Sign up to request clarification or add additional context in comments.

8 Comments

Am getting values by the mentioned method using //Sample/*/text(). But I didn't get anything //Sample/abc:priority/text() please advice.
@JohnChristy - It's because the prefix abc isn't bound. If //Sample/*/text() is working, you can try using *[local-name()='priority'] instead of * only.
Am getting null value while using *[local-name()='priority']. actually i want to read a webservice it will look like as follows
<?xml version="1.0" encoding="UTF-8"?> <abc:Event abc1:type="abc:Event" xmlns:abc="schema.abc.com/abc" xmlns:abc1="w3.org/2001/XMLSchema-instance"> <abc:eventID>65c81c27-c79e-ce-9e4b-1d6c36f2f30c</abc:eventID> <abc:sequenceNumber>1</abc:sequenceNumber> <abc:userId>sample</abc:userId> <abc:externalApplicationId>SampleApp</abc:externalApplicationId> <abc:subscriptionId>1e318cb7-84f2-40c6 </abc:subscriptionId> <abc:channelId>714353f-4313-4f5f-aa61-b912c7</abc:channelId> <abc:eventData abc1:type="abc:NewSubscriptionEvent" /> </abc:Event> Sorry for the late reply.
Can i get the user id using Xpath? If so please advice. Thnks
|
1

Try without this line:

    domFactory.setNamespaceAware(true); // never forget this!

Although it normally is a bad idea to run without namespace awareness, in this specific case it makes sense, since the input file is the way it is.

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.