简体   繁体   English

Android XML解析器无法解析

[英]Android XML parser not parsing

I'm trying to parse XML with Android's own parser. 我正在尝试使用Android自己的解析器解析XML。 I am using the IBM tutorial as a guide when doing this. 执行此操作时,我将使用IBM教程作为指南。

THE CODE 编码

The XML I'm testing with is with the following link. 我正在测试的XML带有以下链接。 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: 我已经建立了一个名为AndroidSaxFeedParser的解析器类,如下所示:

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: 上面的类扩展的BaseFeedParser如下所示:

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) (我不包括Message类,因为问题不应该在那里,它永远不会有机会处理消息)

THE PROBLEM 问题

None of the listeners in the AndroidSaxFeedParser ever hear anything. AndroidSaxFeedParser中的听众都听不到任何声音。 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). 我看到Elements网站和网站都被正确选中(我可以在变量中看到相关数据)。 It runs down to the final Xml.parse() line which executes properly but returns a blank result. 它运行到最后的Xml.parse()行,该行可以正确执行,但返回空白结果。

Any ideas where I'm going wrong? 有什么想法我要去哪里吗? Thanks! 谢谢!

I tried to follow the IBM tutorial, and found it futile. 我尝试遵循IBM教程,但发现它没有用。

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. 最后,我找到了一个新示例并编写了本教程,它解析RSS提要,但相同的原理适用于任何XML。

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

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 . 好吧,我无法使其与Android的SAX解析器一起使用,因此,根据IBM的示例 ,我改用XmlPullFeedParser类。 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. 我将发布代码,但是在测试它时,我也切换到了另一个XML调用,因此解析与上面的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM