【问题标题】:How to parse only the children where attribute = foo from xml using android.sax?如何使用android.sax仅解析xml中attribute = foo的子项?
【发布时间】:2011-09-01 21:06:51
【问题描述】:

我有一些数据存储在一个 xml 文件中,如下所示:

<group name="example">
    <article name="foo">
        <content>Lorem ipsum dolor sit amet.</content>
        <link>
            <title>Lipsum</title>
            <url>http://www.lipsum.com/feed/html</url>
        </link>
    </article>
    <article name="bar">
        <content>Lorem ipsum dolor sit amet.</content>
        <link>
            <title>Google</title>
            <url>http://www.google.com</url>
        </link>
    </article>
</group>

说我要解析具体的文章name="foo"在android中。到目前为止,我一直在使用 android.sax 包,但我不知道它是否具有此功能。或者,如果有其他方法可以做到这一点,我将不胜感激。

编辑: 这是我到目前为止的代码。我觉得我错过了什么。问题是它从所有文章中添加信息,而不仅仅是我想要的特定文章。

public Article parse(final String articleTitle) {
    RootElement root = new RootElement("learning");

    Element group = root.getChild("group");

    final Element article = group.getChild("article");

    article.setStartElementListener(new StartElementListener() {

        @Override
        public void start(Attributes attributes) {
            if (attributes.getValue("name").equals(articleTitle)) {
                currentArticle = new Article();
                currentArticle.setTitle(articleTitle);
                setupArticle(currentArticle);
            }
        }

        private void setupArticle(final Article currentArticle) {

            article.getChild("content").setEndTextElementListener(
                    new EndTextElementListener() {

                        @Override
                        public void end(String body) {
                            currentArticle.setContent(body);
                        }

                    });


    Context context = Application.getAppContext();
    InputStream raw;

    try {
        raw = context.getAssets().open("learning_articles.xml");
        Xml.parse(raw, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return currentArticle;

【问题讨论】:

标签: android xml xml-parsing sax


【解决方案1】:

这不是一个确切的答案,但是当我使用 Groovy(能够使用 sax)时,我做了类似于这里所做的事情:

我没有亲自用 Android 做过,但是看到 Android、Java 和 Groovy 在语法上相对相似,从那里找到答案应该不难!

顺便说一下,它在 Java 和 Groovy 中都有示例;)(您可能对清单两个感兴趣)

【讨论】:

    【解决方案2】:

    您正在为 &lt;content&gt; 元素设置 EndTextElementListener,此时解析器实际上 解析 父 (&lt;article&gt;) 元素。只需在调用Xml.parse() 方法之前设置所有侦听器并将条件添加到每个侦听器。

    我会这样说:

    private static Article currentArticle;
    
    public Article parse(final String articleTitle) {
    RootElement root = new RootElement("learning");
    Element group = root.getChild("group");
    final Element article = group.getChild("article");
    
    article.setStartElementListener(new StartElementListener() {
    
        @Override
        public void start(Attributes attributes) {
            if (attributes.getValue("name").equals(articleTitle)) {
                currentArticle = new Article();
                currentArticle.setTitle(articleTitle);
            } else {
                currentArticle = null;
            }
        }
    });
    
    article.getChild("content").setEndTextElementListener(
                    new EndTextElementListener() {
    
         @Override
         public void end(String body) {
            if (currentArticle.getTitle().equals(articleTitle)) {
               currentArticle.setContent(body);
            }
         }
    
    });
    
    Context context = Application.getAppContext();
    InputStream raw;
    
    try {
        raw = context.getAssets().open("learning_articles.xml");
        Xml.parse(raw, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    
    return currentArticle;
    

    或者,如果您想解析多篇文章,请将 currentArticle 添加到 List 元素末尾的 &lt;article&gt; 并返回整个 Collection 而不是一个对象。

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多