鉴于像这样的xml文件,我如何将这些数据解析为arraylist?
<Providers>
<CarDealer name="BEFORWARD" id="1">
<CarMake name="Toyota" id="20" stype="se01">
<CarModel name="Belta" id="21"/>
<CarModel name="RunX" id="22"/>
<CarModel name="Corolla" id="23"/>
</CarMake>
<CarMake name="Nissan" id="30" stype="se02">
<CarModel name="Murano" id="31"/>
<CarModel name="Pathfinder" id="32"/>
<CarModel name="Navara" id="33"/>
</CarMake>
</CarDealer>
解析数据应该像这样存储在arraylist中:
(stype, id, CarMake_name, id, CarModel_name, id, CarModel_name, id, CarModel_name)
(stype, id, CarMake_name, id, CarModel_name, id, CarModel_name, id, CarModel_name)
使用SAXParser,这是我尝试做的,但是当我打印出来时,我的arraylist没有显示任何内容。任何帮助和提示将不胜感激;我是Android开发的新手:
public class SAXXMLHandler extends DefaultHandler {
private static List<Service> services = new ArrayList<Service>();
private Service serv = null;
public String text = null;
public List<Service> getService() {
return services;
}
@Override
// A start tag is encountered.
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
switch (qName) {
// Create a new Service.
case "CarMake": {
serv = new Service();
serv.setID(Integer.parseInt(attributes.getValue("id")));
serv.setName(attributes.getValue("name"));
serv.setStype(attributes.getValue("stype"));
break;
}
}
}
public void endElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "CarMake": {
// The end tag of an service was encountered, so add the service to the list.
services.add(serv);
break;
}
case "CarModel": {
serv.setName(attributes.getValue("name"));
serv.setID(Integer.parseInt(attributes.getValue("id")));
break;
}
}
}