简体   繁体   English

使用android sax解析器解析XML

[英]Parsing XML using android sax parser

below is a sample structure of the XML I'm trying to parse using the Android SAX parsing approach in this tutorial . 以下是本教程中我尝试使用Android SAX解析方法解析的XML的示例结构。

<root>
    <parent>
        <id></id>
        <name></name>
        <child>
            <id></id>
            <name></name>
        </child>
        <child>
            <id></id>
            <name> </name>
        </child>
    </parent>
    <parent>
        <id></id>
        <name></name>
        <child>
            <id></id>
            <name></name>
        </child>
    </parent>
    ...
</root>

I have two classes named Parent and Child . 我有两个名为ParentChild课程。 Parent has a field which is a list of Child objects, such as this. 父级具有一个字段,该字段是诸如此类的子级对象的列表。

Parent 父级

public class Parent {

    private String id;
    private String name;
    private List<Child> childList;

    //constructor, getters and setters

    // more code

}

Child 儿童

public class Child {

    private String id;
    private String name;

    //constructor, getters and setters

    // more code

}

So I created a Parent object to store the parsed data. 因此,我创建了一个Parent对象来存储解析后的数据。 In the code below I can get the name and id element of parent but I cant figure out how to parse the child elements and their own child elements. 在下面的代码中,我可以获取parentnameid元素,但无法弄清楚如何解析child元素及其自己的子元素。 I dont know if this is the right way to accomplish what I'm trying to do. 我不知道这是否是完成我想做的正确方法。

Can someone show me a way? 有人可以告诉我一种方法吗?

public class AndroidSaxFeedParser extends BaseFeedParser {

    public AndroidSaxFeedParser(String feedUrl) {
        super(feedUrl);
    }

    public List<Parent> parse() {

        final Parent current = new Parent();
        RootElement root = new RootElement("root");
        final List<Parent> parents = new ArrayList<Parent>();
        Element parent = root.getChild("parent");

        parent.setEndElementListener(new EndElementListener() {
            public void end() {
                parents.add(current.copy());
            }
        });

        parent .getChild("id").setEndTextElementListener(
                new EndTextElementListener() {
            public void end(String body) {
                current.setId(body);
            }
        });

        parent .getChild("name").setEndTextElementListener(
                new EndTextElementListener() {
            public void end(String body) {
                current.setName(body);
            }
        });

        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
                    root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return parents ;
    }
}
parent.getChild("child").getChild("id").setEndTextElementListener(
                new EndTextElementListener() {
            public void end(String body) {
                current.setChildId(body);
            }
        });
parent.getChild("child").getChild("name").setEndTextElementListener(
                new EndTextElementListener() {
            public void end(String body) {
                current.setChildName(body);
            }
        });

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

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