从网站解析XML在Android的String数组,请帮助我 [英] Parsing XML from a website to a String array in Android please help me

查看:152
本文介绍了从网站解析XML在Android的String数组,请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在做一个Android应用程序,拉从一个Wiki的一些数据,起初我打算寻找一种方法来解析HTML的过程,但是从事情,有人向我指出的是,XML会更容易的工作。现在,我坚持试图找到一种方法来正确地解析XML。我想,现在从网络地址解析来自:

Hello I am in the process of making an Android app that pulls some data from a Wiki, at first I was planning on finding a way to parse the HTML, but from something that someone pointed out to me is that XML would be much easier to work with. Now I am stuck trying to find a way to parse the XML correctly. I am trying to parse from a web address right now from:

<一个href=\"http://zelda.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Games&cmlimit=500&format=xml\" rel=\"nofollow\">http://zelda.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Games&cmlimit=500&format=xml

我试图让每一个游戏的标题为一个字符串数组,我遇到了一些麻烦。我没有code,我尝试的一个例子,它是通过使用xmlpullparser。我的应用程序崩溃每次我试图用它做任何事情。它会更好保存到本地XML,并从那里解析?或者我会好起来的,从网址去?我将如何去正确地分析此为一个字符串数组?请帮我,感谢您抽出宝贵的时间来阅读。

I am trying to get the titles of each of the games into a string array and I am having some trouble. I don't have an example of the code I was trying out, it was by using xmlpullparser. My app crashes everytime that I try to do anything with it. Would it be better to save the XML locally and parse from there? or would I be okay going from the web address? and how would I go about parsing this correctly into a string array? Please help me, and thank you for taking the time to read this.

如果您需要查看code或任何东西,我可以得到它今晚稍后,我只是不靠近我的电脑在这个时候。谢谢你。

If you need to see code or anything I can get it later tonight, I am just not near my PC at this time. Thank you.

推荐答案

无论何时你发现自己写解析器code简单的格式,比如一个在你的例子你几乎的总是的做一些事情错误的,不使用合适的框架。

Whenever you find yourself writing parser code for simple formats like the one in your example you're almost always doing something wrong and not using a suitable framework.

例如 - 有解析包含在SDK中的 android.sax 封装XML一组简单的帮手,它只是碰巧你张贴的例子可以很容易分析是这样的:

For instance - there's a set of simple helpers for parsing XML in the android.sax package included in the SDK and it just happens that the example you posted could be easily parsed like this:

public class WikiParser {
    public static class Cm {
        public String mPageId;
        public String mNs;
        public String mTitle;
    }
    private static class CmListener implements StartElementListener {
        final List<Cm> mCms;
        CmListener(List<Cm> cms) {
            mCms = cms;
        }
        @Override
        public void start(Attributes attributes) {
            Cm cm = new Cm();
            cm.mPageId = attributes.getValue("", "pageid");
            cm.mNs = attributes.getValue("", "ns");
            cm.mTitle = attributes.getValue("", "title");
            mCms.add(cm);
        }
    }
    public void parseInto(URL url, List<Cm> cms) throws IOException, SAXException {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        try {
            parseInto(new BufferedInputStream(con.getInputStream()), cms);
        } finally {
            con.disconnect();
        }
    }
    public void parseInto(InputStream docStream, List<Cm> cms) throws IOException, SAXException {
        RootElement api = new RootElement("api");
        Element query = api.requireChild("query");
        Element categoryMembers = query.requireChild("categorymembers");
        Element cm = categoryMembers.requireChild("cm");
        cm.setStartElementListener(new CmListener(cms));
        Xml.parse(docStream, Encoding.UTF_8, api.getContentHandler());
    }
}

基本上,像这样调用:

Basically, called like this:

WikiParser p = new WikiParser();
ArrayList<WikiParser.Cm> res = new ArrayList<WikiParser.Cm>();
try {
    p.parseInto(new URL("http://zelda.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Games&cmlimit=500&format=xml"), res);
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (SAXException e) {}

编辑:这是你如何创建一个列表与LT;弦乐&GT; 而不是:

This is how you'd create a List<String> instead:

public class WikiParser {
    private static class CmListener implements StartElementListener {
        final List<String> mTitles;
        CmListener(List<String> titles) {
            mTitles = titles;
        }
        @Override
        public void start(Attributes attributes) {
            String title = attributes.getValue("", "title");
            if (!TextUtils.isEmpty(title)) {
                mTitles.add(title);
            }
        }
    }
    public void parseInto(URL url, List<String> titles) throws IOException, SAXException {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        try {
            parseInto(new BufferedInputStream(con.getInputStream()), titles);
        } finally {
            con.disconnect();
        }
    }
    public void parseInto(InputStream docStream, List<String> titles) throws IOException, SAXException {
        RootElement api = new RootElement("api");
        Element query = api.requireChild("query");
        Element categoryMembers = query.requireChild("categorymembers");
        Element cm = categoryMembers.requireChild("cm");
        cm.setStartElementListener(new CmListener(titles));
        Xml.parse(docStream, Encoding.UTF_8, api.getContentHandler());
    }
}

和则:

WikiParser p = new WikiParser();
ArrayList<String> titles = new ArrayList<String>();
try {
    p.parseInto(new URL("http://zelda.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Games&cmlimit=500&format=xml"), titles);
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (SAXException e) {}

这篇关于从网站解析XML在Android的String数组,请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆