2013-02-19 58 views
0

我是android新手,我在android.I中寻找sax解析,搜索时有太多示例,但没有一个符合我的要求,所以我在这里张贴我拥有的XML结构和我的要求。如何在android中通过引用父节点来解析sax解析

<vehicle_appname version="1.0">

<vehicle_selection> 

<vehicle_selection_option name="car"> 

<vehicle_issues> 
<vehicle_item id="1" vehicle_filename="honda" vehicle_fileurl="honda13" icon_image="hodaFeb13.png" vehicle_heading="1.png" chapter_count="45"/> 
<vehicle_item id="2" vehicle_filename="benz" vehicle_fileurl="Woman_feb13" icon_image="benzfeb13.png" vehicle_heading="1.png" chapter_count="6"/> 
</vehicle_issues> 

</vehicle_selection_option> 

<vehicle_selection_option name="jeep"> 

<vehicle_issues> 
<vehicle_item id="1" vehicle_filename="mahindra" vehicle_fileurl="mahindra" icon_image="mahindra_Feb13.png" vehicle_heading="1.png" chapter_count="45"/> 

</vehicle_issues> 


</vehicle_selection_option> 

</vehicle_selection> 
</vehicle_appname> 

这里vehicle_selection_option名称是一个标题标签和vehicle_item标记包含头内容,可以有单个或多个内容标签。 我的要求:

  1. 我想解析这个XML。我知道XML sax解析很容易,但是在这里我嵌套了xml文件。因此,车辆标记中的值必须适合其标头(vehicle_selection_option标记)。
  2. 我想在列表视图中显示此内容。

回答

0

覆盖startElementendElement是你所需要的。实际上startElement会通知一个元素的开始。同时endElement会通知元素的结尾。所以,你可以跟踪当vehicle_selection_option打开和关闭

事情是这样的:检查是否有错字

boolean newCar = false; 
@override 
public void startElement(String uri, String localName,String qName, 
       Attributes attributes) throws SAXException { 
    if (qName.equalsIgnoreCase("vehicle_selection_option")) 
     newCar = true; 
} 

@override 
public void endElement(String uri, String localName,String qName, 
       Attributes attributes) throws SAXException { 
    if (qName.equalsIgnoreCase("vehicle_selection_option")) 
     newCar = false; 
} 
+0

你能帮我举个例子吗? – Zubair 2013-02-19 11:43:44

0

检查localValue(的startElement参数)上的startElement方法vlaue“vehicle_selection_option”和创建的对象Vechile(用于存储车辆信息)以及在endElement mothod中检查相同的字符串,并将此对象添加到集合(即ArrayList)中。解析整个数据后,您将获得集合中的所有vechile对象(ArrayList)。

+0

你解决了你的问题吗? – Jitendra 2013-02-22 11:25:19