views:

952

answers:

1

I am using SAX to parse an XML file I'm pulling from the web. I've extended DefaultHandler with code similar to:

public class ArrivalHandler extends DefaultHandler {    
    @Override
    public void startElement(String namespaceUri, String localName, String qualifiedName, Attributes attributes) throws SAXException {
        if (qualifiedName.equalsIgnoreCase("resultSet")) {
            System.out.println("got a resultset");
        } else if (qualifiedName.equalsIgnoreCase("location")) {
            System.out.println("got a location");
        } else if (qualifiedName.equalsIgnoreCase("arrival")) {
            System.out.println("got an arrival");
        } else {
            System.out.println("There was an unknown XML element encountered: '" + qualifiedName + "'");
        }        
    }

    @Override
    public void endElement(String namespaceUri, String localName, String qualifiedName) throws SAXException {
        // we'll just ignore this for now
    }

    @Override
    public void characters(char[] chars, int startIndex, int length) throws SAXException {
        // ignore this too
    }
}

The problem I'm having is that I'm just getting a series of empty elements. The log reads:

There was an unknown XML element encountered: ''
There was an unknown XML element encountered: ''
There was an unknown XML element encountered: ''
etc

This worked fine when I was just passing parser.parse a local file, but now I'm pulling it from the web with:

HttpClient httpClient = new DefaultHttpClient();
resp = httpClient.execute("http://example.com/whatever");

SAXParserFactory saxFactory = SAXParserFactory.newInstance();
ArrivalHandler handler = new ArrivalHandler();
SAXParser parser = saxFactory.newSAXParser();
parser.parse(resp.getEntity().getContent(), handler);

and I get the (apparently) empty results described above.

What I've looked into so far:

  1. I converted the InputStream from resp.getEntity().getContent() to a string and dumped it out and it looks like I'm getting the XML from the server correctly.
  2. There are no exceptions thrown but there is a warning that reads "W/ExpatReader(232): DTD handlers aren't supported.".

Any other ideas for what I'm doing incorrectly or how to debug this?

+2  A: 

From the docs for ContentHandler.startElement:

the qualified name is required when the namespace-prefixes property is true, and is optional when the namespace-prefixes property is false (the default).

So, do you have the namespace-prefixes property set to true?

Can you just cope with the uri and localName instead?

Jon Skeet
D'oh. `localName` gave my what I wanted. I don't understand why it's returning them differently in the different contexts though (reading from local vs remote).
fiXedd
Same thing happens to me when using Android. It's not related to "pulling it from the web".In my junit tests of the library, it sets qualifiedName.But when i use this library in an Android project, it sets localName instead of qualifiedName.Don't know why :(
gaizka