【问题标题】:Android SAX Parser - Same tag names on several levelsAndroid SAX Parser - 多个级别的相同标签名称
【发布时间】:2013-03-14 16:30:14
【问题描述】:

我一直在寻找一种解决方案,以解析在多个级别上具有相同标签名称的 xml。这是我必须处理的 XML 示例(这些部分不是静态的):

<xml>
  <section id="0">
     <title>foo</title>
     <section id="1">
        <title>sub foo #1</title>
        <section id="2">
          <title>sub sub foo</title>
        </section>
     </section>
     <section id="3">
        <title>sub foo #2</title>
     </section>
  </section>
<xml>

我一直在尝试几种可能性,例如尝试列表、堆栈,但是我对 SAX 所做的事情还没有产生任何正确的结果;换句话说,我被困住了:(

我创建了一个名为 Section 的类:

public class Section {
public String id;
public String title;
public List<Section> sections; }

我想知道是否还应该添加父变量?

public Section parent;

如果有人有解决方案,我非常感谢! :D

【问题讨论】:

  • 您对如何生成 xml 有任何影响吗?我的猜测是 SAX 解析器正在尝试执行父/子逻辑,并且由于所有内容都被命名为“部分”,因此它无法正确建立节点之间的关系。
  • 不幸的是我没有。是的,SAX 解析器实际上会在每个节节点之后覆盖我的 Section 对象。我已经考虑过在父节节点中使用布尔变量将其设置为 true 一次,但是一旦解析器进入下一个节节点,布尔变量就没有用了。但是,我对我使用的解析器有影响,所以如果你知道一个可以解决这个问题的好的解析器,我一定会接受它!

标签: android tags sax saxparser


【解决方案1】:

确实,您可能至少需要一个堆栈。

(我希望)对您的 Section 类(setter/getter 和添加部分的方法)进行明确的更改,这个处理程序似乎可以解决问题:

由于您的布局似乎允许在 &lt;xml&gt; 根目录下紧接多个 &lt;section&gt; 标签,因此我已实现它并将结果放入 List&lt;Section&gt;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class SectionXmlHandler extends DefaultHandler {

    private List<Section> results;
    private Stack<Section> stack;
    private StringBuffer buffer = new StringBuffer();

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("xml".equals(localName)) {
            results = new ArrayList<Section>();
            stack = new Stack<Section>();
        } else if ("section".equals(localName)) {
            Section currentSection = new Section();
            currentSection.setId(attributes.getValue("id"));
            stack.push(currentSection);
        } else if ("title".equals(localName)) {
            buffer.setLength(0);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("section".equals(localName)) {
            Section currentSection = stack.pop();
            if (stack.isEmpty()) {
                results.add(currentSection);
            } else {
                Section parent = stack.peek();
                parent.addSection(currentSection);
            }
        } else if ("title".equals(localName)) {
            Section currentSection = stack.peek();
            currentSection.setTitle(buffer.toString());
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        buffer.append(ch, start, length);
    }

    public List<Section> getResults() {
        return results;
    }
}

【讨论】:

  • 谢谢唐!这就像一个魅力。 :) 我需要阅读 Stacks 的文档才能完全理解它,但是从您提交的内容来看,对我来说似乎更清楚了!至于section类,我确实写了getters/setters。
猜你喜欢
  • 2012-07-06
  • 2014-01-18
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2011-09-25
  • 2012-03-17
  • 1970-01-01
相关资源
最近更新 更多