2016-08-15 113 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

希望它可以幫助人誰也有類似的問題。