3

Consider the following input xml document:

<oracle:EMP xmlns:oracle="http://www.oracle.com/xml"/>

...and the following handler:

final class XMLizatorSaxHandler extends DefaultHandler {
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        System.out.println(uri + "," + localName + "," + qName);
    }
}

When using it with a SAXParser I would expect the following outupt:

uri: http://www.oracle.com/xml
localName: EMP
qName: oracle:EMP

But I get this instead:

uri:
localName:
qName: oracle:EMP

Why? How can I get correct information?

lviggiani
  • 5,824
  • 12
  • 56
  • 89
  • 2
    Do you have the namespace feature enabled in your parser configuration? You need to set the http://xml.org/sax/features/namespaces in order for namespaces to be guaranteed to be reported. – Steve Jul 22 '14 at 15:26
  • How do I check that? is that a SAXParser setting or what? – lviggiani Jul 22 '14 at 15:31
  • Ok found, need to call saxParserFactory.setNamespaceAware(true) before saxParserFactory.newSAXParser(). – lviggiani Jul 22 '14 at 15:34

1 Answers1

5

Ok, thanks to Steve's hint I found the solution.

Need to call SaxParserFactory.setNamespaceAware(true); before SaxParserFactory.newSAXParser();

Here is full code:

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true); // here is the trick

SAXParser parser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();

parser.parse(in, handler);
lviggiani
  • 5,824
  • 12
  • 56
  • 89
  • 1
    The fact that SAXParserFactory and DocumentBuilderFactory default to non-namespace-aware parsing always seemed to me like a mistake in the specs, but I guess we're stuck with it now for backwards compatibility reasons. – Ian Roberts Jul 22 '14 at 15:45