29

Org.xml.sax.SAXParseException when parsing XMl using XPATH

 4 years ago
source link: https://www.codesd.com/item/org-xml-sax-saxparseexception-when-parsing-xml-using-xpath.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Org.xml.sax.SAXParseException when parsing XMl using XPATH

advertisements

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?


"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>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK