【发布时间】:2016-05-29 14:48:46
【问题描述】:
我想使用 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();
}
我被困在这里了。请帮帮我。
【问题讨论】:
-
对于这么小的 XML,为什么不使用对用户更友好的内存解析器呢?
标签: android listview sax saxparser android-webservice