2012-03-21 8 views
0

ネストされたXMLを解析できません。これは、各ノードは、AndroidでXMLを解析するための最良の方法は、SAXパーサーを使用している属性と子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クラスを作るか、1つ以上の私を正しくガイドしてください私は非常に疲れています.... – user1282588

+0

バディ私はmp3、lrcを表すために3クラスを必要としますid、mp3およびlrc; –

1

を持つ、ノードを入れ子にしています。 AndroidはSDKに定義されたこのライブラリを持っています。あなたはチュートリアルを行くことができます:あなたがここに例を得ることができます SAXParser

:解析中にあなたがすべての問題/問題がある場合は
Android_XML_SAX_Parser_Example
android-sdk-build-a-simple-sax-parser
android-sax-parsing-example

あなたはここに投稿することができます。 SAXパーサはイベントベースであるため、オーバーライドされたDefaultHandlerクラスのstartElement()でネストされたタグを読み取る必要があるため、ネストされたxmlの解析は問題になりません。

+0

ここで作成する必要がありますどのように多くのPOJOクラスの一つだけをクリアしてください、どのようにセットそのプロパティ.. – user1282588

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

関連する問題