我想使用SAX Parser 解析 XML数据,并在列表视图中显示已解析的数据。
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();
}
我被困在这里。请帮帮我。
答案 0 :(得分:2)
有一点你应该看看:
如果没有,请确保XML解析工作正常。
根据您的代码,我认为您应该在&#34; endElement&#34;中添加案例。功能如下:
else if (localName.equalsIgnoreCase("item"))
product_list.add(productdto);
希望我的回答对你有帮助,抱歉我的英语太糟糕了:P