2012-03-21 238 views
0

我無法解析嵌套的XML。它嵌套節點,其屬性和子在android中解析嵌套的XML

<navigation-bar title = "" color = "#7eb432" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 

    <tab-bar numberOfTabs = "4" > 
     <tab-bar-item title = "Home" image = "tab_home.png" linkedScreen = "101" /> 
     <tab-bar-item title = "Calendar" image = "tab_calendar.png" linkedScreen = "102" /> 
     <tab-bar-item title = "Menu" image = "tab_menu.png" linkedScreen = "604" /> 
     <tab-bar-item title = "Directions" image = "tab_directions.png" linkedScreen = "401" /> 
     <tab-bar-item title = "Contact" image = "tab_contact.png" linkedScreen = "206" /> 
    </tab-bar> 

</screen> 

<screen id = "101" backgroundColor = "" backgroundImg = "HomeScreenBg.png" templateId = "11" hasNavigationBar = "0" hasTabBar = "1" 
cresInfoButton = "1" > 

    <navigation-bar title = "" color = "#7eb432" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 
    <button-view yOffset = "100" spacing = "6" /> 
    <button width = "274" height = "30" image = "blue_home_button.png" action = "201" textColor = "#ffffff">About The Clifton School</button> 
    <button width = "274" height = "30" image = "blue_home_button.png" action = "102" textColor = "#ffffff">School Calendar</button> 
    <button width = "274" height = "30" image = "blue_home_button.png" action = "103" textColor = "#ffffff">Admissions</button> 
    <button width = "274" height = "30" image = "red_home_button.png" action = "601" textColor = "#ffffff">Parent Corner</button> 
    <button width = "274" height = "30" image = "green_home_button.png.png" action = "301" textColor = "#ffffff">Request Information</button> 
    <button width = "274" height = "30" image = "green_home_button.png.png" action = "401" textColor = "#ffffff">Directions</button> 
    <button width = "274" height = "30" image = "green_home_button.png.png" action = "112" textColor = "#ffffff">Tell a Friend about Clifton</button> 

</screen> 

回答

0

我認爲你知道這個SAX解析器,你所得到的困惑是解析那些正在重複的節點。對 ?問題是解析時存儲每條記錄的位置。那麼使用startElement()和endElement()方法,你可以使用嵌套元素的任何種類的對象的ArrayList。

+0

好吧,先生,但我只有一個pojo類或多個請正確引導我,我很累.... – user1282588 2012-03-21 07:59:02

+0

好友我研究了你的xml反應,你需要3個類來表示mp3,lrc和last id,mp3和lrc; – 2012-03-29 12:54:31

1

最佳方式,每個節點的Android解析XML使用SAX解析器。 Android在其SDK中定義了此庫。你可以通過教程: SAXParser

你可以在這裏的例子:
Android_XML_SAX_Parser_Example
android-sdk-build-a-simple-sax-parser
android-sax-parsing-example

如果您有任何問題/問題,同時解析您可以張貼在這裏。解析嵌套xml不是問題,因爲SAX解析器是基於事件的,您必須讀取重寫的DefaultHandler類的startElement()中的嵌套標記。

+0

請清除只有一件事多少POJO類應該在這裏創建,以及如何設置它的屬性.. – user1282588 2012-03-21 06:03:36

0

您可以在父節點是這樣設置的標誌:

public void characters(char[] ch, int start, int length) 
      throws SAXException { 
     // TODO Auto-generated method stub 
     String temp=new String(ch,start,length); 

     if (tagName.equals("id")){ 
      mp3Info.setId(temp); 

     } 

     else if(tagName.equals("mp3")){ 
      flag=1; 
//   System.out.println(temp); 
//   
//   mp3Info.setMp3Name(temp); 
     } 
     else if (tagName.equals("name") && flag==1){ 
      mp3Info.setMp3Name(temp); 
     } 
     else if (tagName.equals("size") && flag==1){ 
      mp3Info.setMp3Size(temp); 
     } 
     else if(tagName.equals("lrc")){ 
      flag=2; 
     } 
     else if(tagName.equals("name")&& flag==2){ 
      mp3Info.setLrcName(temp); 
     } 
     else if(tagName.equals("size") && flag==2){ 
      mp3Info.setLrcSize(temp); 
     } 
    } 

的XML是:

<resources> 
    <resource> 
     <id>0001</id> 
     <mp3> 
      <name>Baby</name> 
      <size>8586816</size> 
     </mp3> 
     <lrc> 
      <name>Baby.lrc</name> 
      <size>2385</size> 
     </lrc>  
    </resource> 
    <resource> 
     <id>0002</id> 
     <mp3> 
      <name>Beat It.mp3</name> 
      <size>7259782</size> 
     </mp3> 
     <lrc> 
      <name>Beat It.lrc</name> 
      <size>2536</size> 
     </lrc>  


</resource> 
</resources> 
+0

請清楚startElement&EndElement的結構,請指導我更多......在我的情況下,應該製作多少pojo類?請幫幫我 – user1282588 2012-03-21 06:58:38