在android中解析嵌套的XML
我无法解析嵌套的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>
我认为你知道这个SAX解析器,你所得到的困惑是解析那些正在重复的节点。对 ?问题是解析时存储每条记录的位置。那么使用startElement()和endElement()方法,你可以使用嵌套元素的任何种类的对象的ArrayList。
最佳方式,每个节点的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()中的嵌套标记。
请清除只有一件事多少POJO类应该在这里创建,以及如何设置它的属性.. – user1282588 2012-03-21 06:03:36
您可以在父节点是这样设置的标志:
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>
请清楚startElement&EndElement的结构,请指导我更多......在我的情况下,应该制作多少pojo类?请帮帮我 – user1282588 2012-03-21 06:58:38
好吧,先生,但我只有一个pojo类或多个请正确引导我,我很累.... – user1282588 2012-03-21 07:59:02
好友我研究了你的xml反应,你需要3个类来表示mp3,lrc和last id,mp3和lrc; – 2012-03-29 12:54:31