这个格式的xml,用pull解析器怎么执行了这句就抛出异常了啊,为啥,请教路过的高手!!!!!!!!!!
eventType = parser.next(); food.xml
<?xml version="1.0" encoding="GB2312" ?>
- <DataSets>
- <item>
<name>安格斯雪花牛肉1</name>
<id>2</id>
<price>16</price>
<unit>件</unit>
</item>
- <item>
<name>安格斯雪花牛肉2</name>
<id>2</id>
<price>16</price>
<unit>件</unit>
</item>
- <item>
<name>安格斯雪花牛肉3</name>
<id>2</id>
<price>16</price>
<unit>件</unit>
</item>
</DataSets>
pull解析代码:
package com.xml;import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser; import com.bean.Food;import android.util.Xml; public class PullXMLReader { public static List<Food> readXML(InputStream inStream) { XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(inStream, "GB2312"); int eventType = parser.getEventType(); Food currentFood = null; List<Food> food = null; while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) { case XmlPullParser.START_DOCUMENT://文档开始事件,可以进行数据初始化处理 food = new ArrayList<Food>(); break; case XmlPullParser.START_TAG://开始元素事件 String name = parser.getName(); if (name.equalsIgnoreCase("item")) { currentFood = new Food(); } else if (currentFood != null) { if (name.equalsIgnoreCase("id")) { currentFood.setId(new Integer(parser.nextText()));// 如果后面是Text元素,即返回它的值 } else if (name.equalsIgnoreCase("FoodTypeID")) { currentFood.setFoodtypeid(new Integer(parser.nextText())); } else if (name.equalsIgnoreCase("name")) { currentFood.setName(parser.nextText()); } else if (name.equalsIgnoreCase("unit")) { currentFood.setUnit(parser.nextText()); } else if (name.equalsIgnoreCase("price")) { currentFood.setPrice(Float.parseFloat(parser.nextText())); } else if (name.equalsIgnoreCase("barcode")) { currentFood.setBarcode(parser.nextText()); }else if (name.equalsIgnoreCase("py")) { currentFood.setPy(parser.nextText()); }} break; case XmlPullParser.END_TAG://结束元素事件 if (parser.getName().equalsIgnoreCase("item") && currentFood != null) { food.add(currentFood); currentFood = null; } break; }
eventType = parser.next();
} inStream.close(); return food; } catch (Exception e) { e.printStackTrace(); } return null; } }
eventType = parser.next(); food.xml
<?xml version="1.0" encoding="GB2312" ?>
- <DataSets>
- <item>
<name>安格斯雪花牛肉1</name>
<id>2</id>
<price>16</price>
<unit>件</unit>
</item>
- <item>
<name>安格斯雪花牛肉2</name>
<id>2</id>
<price>16</price>
<unit>件</unit>
</item>
- <item>
<name>安格斯雪花牛肉3</name>
<id>2</id>
<price>16</price>
<unit>件</unit>
</item>
</DataSets>
pull解析代码:
package com.xml;import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser; import com.bean.Food;import android.util.Xml; public class PullXMLReader { public static List<Food> readXML(InputStream inStream) { XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(inStream, "GB2312"); int eventType = parser.getEventType(); Food currentFood = null; List<Food> food = null; while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) { case XmlPullParser.START_DOCUMENT://文档开始事件,可以进行数据初始化处理 food = new ArrayList<Food>(); break; case XmlPullParser.START_TAG://开始元素事件 String name = parser.getName(); if (name.equalsIgnoreCase("item")) { currentFood = new Food(); } else if (currentFood != null) { if (name.equalsIgnoreCase("id")) { currentFood.setId(new Integer(parser.nextText()));// 如果后面是Text元素,即返回它的值 } else if (name.equalsIgnoreCase("FoodTypeID")) { currentFood.setFoodtypeid(new Integer(parser.nextText())); } else if (name.equalsIgnoreCase("name")) { currentFood.setName(parser.nextText()); } else if (name.equalsIgnoreCase("unit")) { currentFood.setUnit(parser.nextText()); } else if (name.equalsIgnoreCase("price")) { currentFood.setPrice(Float.parseFloat(parser.nextText())); } else if (name.equalsIgnoreCase("barcode")) { currentFood.setBarcode(parser.nextText()); }else if (name.equalsIgnoreCase("py")) { currentFood.setPy(parser.nextText()); }} break; case XmlPullParser.END_TAG://结束元素事件 if (parser.getName().equalsIgnoreCase("item") && currentFood != null) { food.add(currentFood); currentFood = null; } break; }
eventType = parser.next();
} inStream.close(); return food; } catch (Exception e) { e.printStackTrace(); } return null; } }
eventType = parser.next(); 就跳出去了,为啥咧