繁体   English   中英

如何使用SAX Parser解析xml数据并将其显示在listview中

[英]How to parse xml data using SAX Parser and show it in listview

我想使用SAX Parser解析XML数据,在listview中显示已解析的数据

XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<category>
   <category_info>
      <parent_title>Shop</parent_title>
      <parent_id>3</parent_id>
      <has_more_items>0</has_more_items>
   </category_info>
   <items>
      <item>
         <label>Citrus</label>
         <entity_id>130</entity_id>
         <next>4</next>
         <subcategory>0</subcategory>
         <content_type>products</content_type>
         <parent_id>128</parent_id>
         <icon>http://foakh.com/media/catalog/category/cache/11/thumbnail/100x/9df78eab33525d08d6e5fb8d27136e95/yyyy.jpg</icon>
      </item>
      <item>
         <label>Apples</label>
         <entity_id>131</entity_id>
         <next>3</next>
         <subcategory>0</subcategory>
         <content_type>products</content_type>
         <parent_id>128</parent_id>
         <icon>http://foakh.com/media/catalog/category/cache/11/thumbnail/100x/9df78eab33525d08d6e5fb8d27136e95/yyyy.jpg</icon>
      </item>
   </items>
</category>

我必须提取标记的数据并将其保存在列表中,然后再在自定义列表视图中显示它。 有一个after_product(product_list)方法,其中数据被带到活动中。 这是用于解析XML数据代码,但是该数据未保存在列表视图中,因此数据没有显示在列表视图中 很明显,我犯了一些错误,但无法弄清楚。 所以任何人都可以帮助我。

 @Override
    public void onSuccess(String response) {
        // TODO Auto-generated method stub

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);
            BufferedReader br = new BufferedReader(new StringReader(response));
            InputSource is = new InputSource(br);
            mXmlReader.parse(is);

        } catch (Exception e) {

            Log.e("TAG", "Exception: " + e.getMessage());
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        tempval = new String(ch, start, length);
        if (b_label == true)
            str_label = str_label + tempval;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if (localName.equalsIgnoreCase("items"))
            product_list = new ArrayList<Products>();
        if (localName.equalsIgnoreCase("item"))
            productdto = new Products();
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if (localName.equalsIgnoreCase("label")) {
            productdto.setLabel(tempval);
            str_label = "";
            b_label = false;
        } else if (localName.equalsIgnoreCase("entity_id"))
            productdto.setEntity_id(tempval);
        else if (localName.equalsIgnoreCase("next"))
            productdto.setNext(tempval);
        else if (localName.equalsIgnoreCase("subcategory"))
            productdto.setSubcategory(tempval);
        else if (localName.equalsIgnoreCase("content_type"))
            productdto.setContent_type(tempval);
        else if (localName.equalsIgnoreCase("parent_id")) {
            if (productdto != null)
                productdto.setParent_id(tempval);
        }
        else if (localName.equalsIgnoreCase("icon"))
                productdto.setIcon(tempval);
        else if (localName.equalsIgnoreCase("items")) {
                ((ProductList) mActivity).after_product(product_list);

            }
    }

方法after_product(product_list)如下:

public void after_product(ArrayList<Products> product_list) {
    productAdapter = new ProductListItemsAdapter(ProductList.this, 0, product_list);
    list_view.setAdapter(productAdapter);
    dialog.dismiss();
}

我被困在这里。 请帮帮我。

有一点要检查:

  1. XML解析工作正常吗?

如果没有,请确保XML解析工作正常。

  1. 解析后将productdto添加到product_list中吗?

根据您的代码,我认为您应该在“ endElement”函数中添加大小写,如下所示:

else if (localName.equalsIgnoreCase("item"))
    product_list.add(productdto);
  1. 确保“ ProductListItemsAdapter”正常运行。

希望我的回答对您有所帮助,对不起,我的英语太差了:P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM