比如解析如下文件
<?xml version="1.0" encoding="utf-8" ?>
<!-- n:Name f:filename d:description p:price-->
<product>
<P n="Dracaena fragrans" f="fb001.png" d="" p="15"/>
<P n="Epipremnum" f="fb002.png" d="" p="15"/>
<P n="Pachira aquatica" f="fb003.png" d="" p="15"/>
<P n="Chrysalidocarpus" f="fb004.png" d="" p="15"/>
<P n="Polyscias" f="fb005.png" d="" p="15"/>
</product>
///
try {
InputStream input =CompanyActivity.this.getAssets().open("product.xml");XmlPullParser parser = Xml.newPullParser();
parser.setInput(input, "UTF-8");
int event = parser.getEventType();
Plant plant=null;
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if ("P".equals(parser.getName())) {
plant=new Plant();
plant.setName(parser.getAttributeValue(0));
plant.setPhotoName(parser.getAttributeValue(1));
plant.setDescription(parser.getAttributeValue(2));
double price=Double.parseDouble(parser.getAttributeValue(3));
plant.setPrice(price);
}
break;
case XmlPullParser.END_TAG:
if ("P".equals(parser.getName()) && parser != null) {
mPlantDal.add(plant);
plant = null;
}
break;
default:
break;
}
event = parser.next();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}