我怎么能在Android的做SAX解析方法是什么? [英] how can I do SAX parsing method in android?

查看:212
本文介绍了我怎么能在Android的做SAX解析方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施android的XML解析。 任何人都可以帮我告诉我必须做些什么来实现一个SAX在我的应用程序分析方法?

I am implementing XML parsing in android. Can anyone help me out by telling what I must do to implement a SAX parsing method in my application?

推荐答案

也许你也可以给Android的自身SAX解析器旋转。我已经用它试图和它的伟大工程。这是基于从IBM网站还样品code

Maybe you can also give Android's own SAX parser a spin. I have tried using it and it works great. This is based on the sample code from the IBM site also.

public class ParseXML {

private URL feedUrl;

public ParseXML(String urlSource){
    try {
        this.feedUrl = new URL(urlSource);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}


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


public YourDataContainer parse(){
    YourDataContainer container = new YourDataContainer();

    RootElement root = new RootElement("root");

    Element firstChild = root.getChild("element1");
    firstChild.getChild("entry1").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            container.setFirstEntry(body);
        }
    });
    firstChild.getChild("entry2").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            container.setSecondEntry(body);
        }
    });


    try {
        Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root.getContentHandler());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return container;
}

如果您的容器具有要保存的价值和领域的getter和setter。

Where YourContainer has fields for the values you want to save and getters and setters.

这将解析是这样的:

<root>
    <element1>
        <entry1>this is</entry1>
        <entry2>a test</entry2>
    </element1>
</root>

目前的运行结束,您的容器将具有了firstEntry =这是和secondEntry =测试

At the end of the run, YourContainer will have firstEntry="this is" and secondEntry="a test".

这篇关于我怎么能在Android的做SAX解析方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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