2016-08-15 114 views
2

我是新来的编码,并试图了解Android的XMLPullParser如何浏览给定的XML。下面是一个XML示例,其中我只对拉取Child 1中的属性信息感兴趣。使用Android XMLPullParser导航/解析XML

如何告诉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> 

谢谢!

教程尝试:

  • 的http:// developer.android.com/training/basics/network-ops/xml.html
  • 的http:// theopentutorials.com/tutorials/android/xml/android-simple-xmlpullparser-tutorial/
+0

拉解析已知很难编码,特别是xml的结构很复杂......你打开其他apis吗? –

+0

对任何其他人开放。我的最终目标是将分析的数据呈现在包含在片段中的列表视图中。 – DC5Gator

+0

你能澄清你想输出的样子吗? –

回答

0

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

我的解决方案来自于使用“完成”标志来解析文档末尾的解析。在我的情况下,一旦解析器检测到上述示例中的Child 2标记,我将done标志设置为“TRUE”。这样子2中的所有重复属性完全被忽略,我只收到子1的属性。

下面是指导是什么给了我使用“完成”标志的想法。

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

希望它可以帮助人谁也有类似的问题。