0
votes

I want to parse XML data using SAX Parser and show the parsed data in the listview.

The XML is given below:-

<?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>

I have to pull the data of tag and save it in the list and after that I have to show it in the custom listview. There is a method after_product(product_list) where the data are taken to the activity. Here is the code for parsing XML data but the data are not saved in the listview and as a result data are not displaying in the listview. It is obvious that I have done some mistake but can't figure it out. So can anybody help me out of this.

 @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);

            }
    }

The method after_product(product_list) is given below:-

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

I have got stucked here. Please help me out.

1
For such a small XML, why not use an in memory parser that is a lot more user friendlier?vtd-xml-author

1 Answers

2
votes

There have some point that you should check out:

  1. XML parse is work well?

If not, make sure XML parse work well.

  1. Is productdto added to the product_list after parsed?

According to your code, I think you should add case in your "endElement" function like this:

else if (localName.equalsIgnoreCase("item"))
    product_list.add(productdto);
  1. Make sure "ProductListItemsAdapter" work well.

Hope my answer is helpful for you,sorry about my English is so bad :P