【问题标题】:How to do sax parsing in android by referencing parent node如何通过引用父节点在android中进行sax解析
【发布时间】:2013-02-19 11:09:48
【问题描述】:

我是 android 新手,我正在寻找 android 中的 sax 解析。我在搜索时有太多示例,但没有一个符合我的要求,所以我在这里发布我拥有的 XML 结构和我的要求。

<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. 我想在列表视图中显示这些内容。

【问题讨论】:

    标签: android


    【解决方案1】:

    在 startElement 方法上使用 vlaue"vehicle_selection_option" 检查 localValue(startElement 的参数),并创建 Vechile 的对象(您用于存储车辆信息),并在 endElement 方法中检查相同的字符串并将此对象添加到集合中(即数组列表)。解析完所有数据后,您将获得集合(ArrayList)中的所有车辆对象。

    【讨论】:

    • 您的问题解决了吗?
    【解决方案2】:

    覆盖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;
     }
    

    【讨论】:

    • 你能帮我举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2015-11-10
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2011-12-05
    • 2011-05-16
    相关资源
    最近更新 更多