使用 Android XMLPullParser 到 Navigate/Parse XML

Using Android XMLPullParser to Navigate/Parse XML

我是编码新手,正在尝试了解 Android 的 XMLPullParser 如何导航给定的 XML。下面是 XML 的示例,我只对提取 Child 1 中的属性信息感兴趣。

如何告诉 Android XMLPullParser 只关心特定标签(子 1)中的信息,而忽略包含类似数据的其他标签?

示例:

<root>
<child 1>
      <child 1.1>
                 <attribute 1></attribute 1>
                 <attribute 2></attribute 2>
      </child 1.1>


      <child 1.2>
                 <attribute 1></attribute 1>
                 <attribute 2></attribute 2>
      </child 1.2>

</child 1>

<child 2>
      <child 2.1>
                 <attribute 1></attribute 1>
                 <attribute 2></attribute 2>
      </child 2.1>

      <child 2.2>
                 <attribute 1></attribute 1>
                 <attribute 2></attribute 2>
      </child 2.2>
</child 2>
</root>

谢谢!

尝试的教程:

通过关注 PullParser 如何循环 XML 找到了答案。通常,我发现 android 开发人员文档中使用的样式很难调整,因此它会比示例更深入地解析一层。

我的解决方案是使用 "done" 标志来缩短文档末尾的解析。在我的例子中,一旦解析器从上面的示例中检测到 Child 2 标记,我将完成标志设置为切换到 "TRUE"。这样子 2 中的所有重复属性都被完全忽略了,我只收到了子 1 的属性。

下面的指南让我产生了使用 "done" 标志的想法。

http://www.ibm.com/developerworks/opensource/library/x-android/index.html

希望对遇到类似问题的人有所帮助。