mattboy
mattboy

Reputation: 2910

Android XML parser not parsing

I'm trying to parse XML with Android's own parser. I am using the IBM tutorial as a guide when doing this.

THE CODE

The XML I'm testing with is with the following link. I am trying to parse Number and Name from it.

https://api.trafiklab.se/sl/realtid/GetSite?stationSearch=Slussen&key=7c4434c36d394e0c99396b8d1c69d9c4

I have set up a parser class called AndroidSaxFeedParser that looks like this:

public class AndroidSaxFeedParser extends BaseFeedParser {

    static final String ROOT = "http://www1.sl.se/realtidws/";
    public AndroidSaxFeedParser(String feedUrl) {
        super(feedUrl);
    }

    public List<Message> parse() {
        final Message currentMessage = new Message();
        RootElement root = new RootElement(ROOT, "Hafas");
        final List<Message> messages = new ArrayList<Message>();
        Element sites = root.getChild(SITES);
        Element site = sites.getChild(SITE);

        site.setEndElementListener(new EndElementListener(){
            public void end() {
                messages.add(currentMessage.copy());
            }
        });

        site.getChild(NUMBER).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setNumber(body);
            }
        });

        site.getChild(NAME).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setName(body);
            }
        });

        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return messages;
    }
}

The BaseFeedParser that the above class extends looks like this:

public abstract class BaseFeedParser implements FeedParser {

    // names of the XML tags
    static final String SITES = "Sites";
    static final  String SITE = "Site";
    static final  String NUMBER = "Number";
    static final  String NAME = "Name";

    private final URL feedUrl;

    protected BaseFeedParser(String feedUrl){
        try {
            this.feedUrl = new URL(feedUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    protected InputStream getInputStream() {
        try {
            return feedUrl.openConnection().getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

I call the parser like this:

AndroidSaxFeedParser parser = new AndroidSaxFeedParser("https://api.trafiklab.se/sl/realtid/GetSite?stationSearch=Slussen&key=7c4434c36d394e0c99396b8d1c69d9c4");
List<Message> messages = parser.parse();

(I'm not including the Message class since the problem shouldn't be there, it never gets a chance to process the messages)

THE PROBLEM

None of the listeners in the AndroidSaxFeedParser ever hear anything. When I place breakpoints inside them, they are never triggered. I see that the Elements sites and site are picked up properly (I can see relevant data in the variables). It runs down to the final Xml.parse() line which executes properly but returns a blank result.

Any ideas where I'm going wrong? Thanks!

Upvotes: 1

Views: 678

Answers (2)

mattboy
mattboy

Reputation: 2910

Well I was unable to get it working with Android's SAX parser so I switched to the XmlPullFeedParser class instead, also according to IBM's examples. This worked without any problems. Cheap solution, but it will have to do.

I'd post the code, but while testing it I also switched to a different XML call so the parsing is different from the above.

Upvotes: 0

Tony
Tony

Reputation: 2425

I tried to follow the IBM tutorial, and found it futile.

In the end I found a new example and wrote this tutorial, it parses a RSS feed but the same principles apply for any XML.

http://droidapp.co.uk/?p=166

Upvotes: 1

Related Questions