SAX parsing problem in Android... empty elements?

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 {    
    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 + "'");

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

    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: ''

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("");

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?

Asked by: Adelaide161 | Posted: 20-01-2022

Answer 1

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?

Answered by: Aida441 | Posted: 21-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android