2

Using SAX and Java I want to parse an XML string but get this exception

[Fatal Error] :1:92: The prefix "xsi" for attribute "xsi:type" associated with an element type "device" is not bound.

    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);

        SAXParser parser = factory.newSAXParser();
        XMLReader xmlReader = parser.getXMLReader();
        handler = new ConfigHandler();

        xmlReader = XMLReaderFactory.createXMLReader();
        // assign our handler
        xmlReader.setContentHandler(handler);
        // perform the synchronous parse
        xmlReader.parse(new InputSource(new StringReader(xml)));

    } catch (Exception e) {
        e.printStackTrace();
    }

Here is the xml

<device  xsi:type="axl:XIPPhone" ctiid="182" uuid="{20a9f66a-fb1f-6981-5851-1474258054dc}">
<fullConfig>true</fullConfig>
<portalDefaultServer>serveraxd.lestry.com</portalDefaultServer>
<deviceProtocol>SOORTY</deviceProtocol>
..
..
</device>

I am not able to change the XML content in this case.

2
  • What is your question? If you have posted the entire XML document (and not just a fragment), the error message explains the problem. The "xsi" namespace prefix is not bound to an actual namespace URI. Commented Jan 7, 2014 at 12:59
  • My question is, what can I do to prevent this exception and allow me to parse the XML file. Commented Jan 7, 2014 at 13:58

1 Answer 1

3

Exactly as the error message says, you have not provided a namespace declaration for the xsi: prefix. Add

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

to your top-level element (the <device> element).

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way possible to parse without adding this. I do not have control over the XML file?
Not as it stands with a modern XML parser, no. You might be able to find an ancient pre-namespaces XML parser, but I really don't recommend it. Your alternative is to do non-XML processing on it to check for and insert the namespace declaration. Or you could wrap incoming documents in additional XML which carries this declaration, but you can't do that for every mistake they're likely to make. Speaking as a pro, I would bounce the document back to its source, with the error message; they need to fix this for their own sakes as well as yours.
This is good to know, I will insert the namespace declaration into the string before processing. The server in question here is very widely deployed and getting them to move on this would be next to impossible in the near term. Thanks all again.

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.