实体类:
public class Food {
int id;
int foodtypeid;
String name;
String unit;
float price;
String issellout;
String py;
String barcode;
String image;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getFoodtypeid() {
return foodtypeid;
}
public void setFoodtypeid(int foodtypeid) {
this.foodtypeid = foodtypeid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getIssellout() {
return issellout;
}
public void setIssellout(String issellout) {
this.issellout = issellout;
}
public String getPy() {
return py;
}
public void setPy(String py) {
this.py = py;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode;
}

}
继承DefaultHandler 的类package com.beatery;
import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import com.bean.Food;
public class XMLContentHandler extends DefaultHandler { private List<Food> foods = null; private Food currentFood; private String tagName = null;//当前解析的元素标签 public List<Food> getFoods() { return foods; } /* * 接收文档的开始的通知。 */ @Override public void startDocument() throws SAXException {  foods = new ArrayList<Food>(); } /* * 接收字符数据的通知。 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { if(tagName!=null){ String data = new String(ch, start, length); if(tagName.equals("id")){ this.currentFood.setId(new Integer(data)); } else if (tagName.equalsIgnoreCase("FoodTypeID")) {  currentFood.setFoodtypeid(new Integer(data)); } else if (tagName.equalsIgnoreCase("name")) {  currentFood.setName(data); } else if (tagName.equalsIgnoreCase("unit")) {  currentFood.setUnit(data); } else if (tagName.equalsIgnoreCase("price")) {  currentFood.setPrice(Float.parseFloat(data)); } else if (tagName.equalsIgnoreCase("barcode")) {  currentFood.setBarcode(data); }else if (tagName.equalsIgnoreCase("py")) {  currentFood.setPy(data); }} } /* * 接收元素开始的通知。 * 参数意义如下: *    namespaceURI:元素的命名空间 *    localName :元素的本地名称(不带前缀) *    qName :元素的限定名(带前缀) *    atts :元素的属性集合 */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if(localName.equals("DataSet")){  currentFood = new Food();  //currentFood.setId(Integer.parseInt(atts.getValue("id"))); } this.tagName = localName; } /* * 接收文档的结尾的通知。 * 参数意义如下: *    uri :元素的命名空间 *    localName :元素的本地名称(不带前缀) *    name :元素的限定名(带前缀) *  */ @Override public void endElement(String uri, String localName, String name) throws SAXException { if(localName.equals("DataSet")){ foods.add(currentFood); currentFood = null; } this.tagName = null; } } 
调用处代码:点击一个按钮调用public void onClick(View arg0) {

File file=new File("/data/data/com.beatery/food.xml");
InputStream inStream = null;
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}List<Food> foods=login.this.readXML(inStream);//同一个类中的方法,下面有该方法

  for(Food food : foods)
  {
  Log.i("dddd", food.getId() + " : " + food.getName() + " : " + food.getBarcode());
  }
}public static List<Food> readXML(InputStream inStream) {     try {  SAXParserFactory spf = SAXParserFactory.newInstance();  SAXParser saxParser = spf.newSAXParser(); //创建解析器  //设置解析器的相关特性,http://xml.org/sax/features/namespaces = true 表示开启命名空间特性   http://xml.org/sax/features/namespaces  saxParser.setProperty("http://xml.org/sax/features/namespaces",true);  XMLContentHandler handler = new XMLContentHandler();  saxParser.parse(inStream, handler);  inStream.close();  return handler.getFoods();     } catch (Exception e) {  e.printStackTrace();     }    return null;  } 
另外food.xml文件格式
<?xml version="1.0" encoding="UTF-8"?>
<DataSet>
<id>2</id>
<FoodTypeID>51</FoodTypeID>
<name>安格斯雪花牛肉</name>
<unit>件</unit>
<price>16</price>
<barcode>6031</barcode>
<py>agsxhnr             </py>
</DataSet>
<DataSet>
<id>2</id>
<FoodTypeID>51</FoodTypeID>
<name>安格斯雪花牛肉</name>
<unit>件</unit>
<price>16</price>
<barcode>6031</barcode>
<py>agsxhnr             </py>
</DataSet>
<DataSet>
<id>2</id>
<FoodTypeID>51</FoodTypeID>
<name>安格斯雪花牛肉</name>
<unit>件</unit>
<price>16</price>
<barcode>6031</barcode>
<py>agsxhnr             </py>
</DataSet>请教路过的高手,这个有错吗?
怎么就调试Food为空?为啥

解决方案 »

  1.   

    loncat日志怎么说啊!贴出来看看!
      

  2.   

    07-07 09:35:51.335: DEBUG/dalvikvm(176): GC_EXPLICIT freed 148 objects / 10704 bytes in 158ms
    07-07 09:35:56.623: WARN/System.err(16148): org.xml.sax.SAXNotRecognizedException: http://xml.org/sax/features/namespaces
    07-07 09:35:56.654: WARN/System.err(16148):     at org.apache.harmony.xml.ExpatReader.setProperty(ExpatReader.java:159)
    07-07 09:35:56.654: WARN/System.err(16148):     at org.apache.harmony.xml.parsers.SAXParserImpl.setProperty(SAXParserImpl.java:89)
    07-07 09:35:56.664: WARN/System.err(16148):     at com.beatery.login.readXML(login.java:283)
    07-07 09:35:56.664: WARN/System.err(16148):     at com.beatery.login$1.onClick(login.java:130)
    07-07 09:35:56.694: WARN/System.err(16148):     at android.view.View.performClick(View.java:2408)
    07-07 09:35:56.694: WARN/System.err(16148):     at android.view.View$PerformClick.run(View.java:8816)
    07-07 09:35:56.755: WARN/System.err(16148):     at android.os.Handler.handleCallback(Handler.java:587)
    07-07 09:35:56.755: WARN/System.err(16148):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-07 09:35:56.755: WARN/System.err(16148):     at android.os.Looper.loop(Looper.java:123)
    07-07 09:35:56.755: WARN/System.err(16148):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-07 09:35:56.764: WARN/System.err(16148):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-07 09:35:56.764: WARN/System.err(16148):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-07 09:35:56.764: WARN/System.err(16148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-07 09:35:56.764: WARN/System.err(16148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-07 09:35:56.764: WARN/System.err(16148):     at dalvik.system.NativeStart.main(Native Method)
    07-07 09:35:56.774: DEBUG/AndroidRuntime(16148): Shutting down VM
    07-07 09:35:56.774: WARN/dalvikvm(16148): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148): FATAL EXCEPTION: main
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148): java.lang.NullPointerException
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at com.beatery.login$1.onClick(login.java:133)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at android.view.View.performClick(View.java:2408)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at android.view.View$PerformClick.run(View.java:8816)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at android.os.Handler.handleCallback(Handler.java:587)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at android.os.Looper.loop(Looper.java:123)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-07 09:35:56.815: ERROR/AndroidRuntime(16148):     at dalvik.system.NativeStart.main(Native Method)
    07-07 09:35:56.874: WARN/ActivityManager(58):   Force finishing activity com.beatery/.login
    07-07 09:35:57.444: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{43f787c0 com.beatery/.login}
    07-07 09:35:59.494: DEBUG/dalvikvm(258): GC_EXPLICIT freed 45 objects / 2048 bytes in 3302ms
    07-07 09:36:08.524: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43f787c0 com.beatery/.login}
      

  3.   

    onClick方法133行有空指针,login.this.readXML(inStream); // 是不是这个获取的为Null呀查看下readXML方法为啥返回值为啥为Null
      

  4.   

    File file=new File("/data/data/com.beatery/food.xml");
    是不是这行的地址写的不对,应该是File file=new File("/data/data/com/beatery/food.xml");
      

  5.   

    应该food.xml没有加载进来所以SAX解析器没有去到值,所以为空
      

  6.   

    food.xml这个已经加载上去了啊,只是里面的显示的中文为乱码?怎么转换咧
      

  7.   

    不是太复杂的XML 建议用XmlPullParser去分析
      

  8.   

    ?xml version="1.0" encoding="UTF-8"?>
    <DataSets>
    <item>
    <ID>2</ID>
    <FoodTypeID>51</FoodTypeID>
    <Name>安格斯雪花牛肉</Name>
    <Unit>件</Unit>
    <Price>16</Price>
    <Price2>0</Price2>
    <Price3>0</Price3>
    <Price4>0</Price4>
    <Cost>0</Cost>
    <MinDiscount>0</MinDiscount>
    <Pos>2006038</Pos>
    <Updated>0</Updated>
    <PrintDelay>0</PrintDelay>
    <IsSellOut>9</IsSellOut>
    <IsDelete>0</IsDelete>
    <CalcID>5</CalcID>
    <PackFoodID>0</PackFoodID>
    <SalerPrice>0</SalerPrice>
    <IsPackFood>0</IsPackFood>
    <BarCode>6031</BarCode>
    <Py>agsxhnr             </Py>
    <ClickCount>0</ClickCount>
    <InteGralCost>0</InteGralCost>
    <InteGralCost2>0</InteGralCost2>
    <TimePriceStyle>0</TimePriceStyle>
    <Stock>99999</Stock>
    <Name2></Name2>
    <IsInpQuantity>0</IsInpQuantity>
    <CalcLabel></CalcLabel>
    <OrderTimeOut>0</OrderTimeOut>
    <CancelTimeOut>0</CancelTimeOut>
    <IsSpecials></IsSpecials>
    <Unit2></Unit2>
    </item>
    </DataSets>
      

  9.   

    这种格式的xml有问题吗??????????????、请教路过的高手啊,怎么不让这个汉字不出乱码啊??xml version="1.0" encoding="UTF-8"?>
    <DataSets>
    <item>
    <ID>2</ID>
    <FoodTypeID>51</FoodTypeID>
    <Name>安格斯雪花牛肉</Name>
    <Unit>件</Unit>
    <Price>16</Price>
    <Price2>0</Price2>
    <Price3>0</Price3>
    <Price4>0</Price4>
    <Cost>0</Cost>
    <MinDiscount>0</MinDiscount>
    <Pos>2006038</Pos>
    <Updated>0</Updated>
    <PrintDelay>0</PrintDelay>
    <IsSellOut>9</IsSellOut>
    <IsDelete>0</IsDelete>
    <CalcID>5</CalcID>
    <PackFoodID>0</PackFoodID>
    <SalerPrice>0</SalerPrice>
    <IsPackFood>0</IsPackFood>
    <BarCode>6031</BarCode>
    <Py>agsxhnr </Py>
    <ClickCount>0</ClickCount>
    <InteGralCost>0</InteGralCost>
    <InteGralCost2>0</InteGralCost2>
    <TimePriceStyle>0</TimePriceStyle>
    <Stock>99999</Stock>
    <Name2></Name2>
    <IsInpQuantity>0</IsInpQuantity>
    <CalcLabel></CalcLabel>
    <OrderTimeOut>0</OrderTimeOut>
    <CancelTimeOut>0</CancelTimeOut>
    <IsSpecials></IsSpecials>
    <Unit2></Unit2>
    </item>
    <item>
    <ID>2</ID>
    <FoodTypeID>51</FoodTypeID>
    <Name>安格斯雪花牛肉</Name>
    <Unit>件</Unit>
    <Price>16</Price>
    <Price2>0</Price2>
    <Price3>0</Price3>
    <Price4>0</Price4>
    <Cost>0</Cost>
    <MinDiscount>0</MinDiscount>
    <Pos>2006038</Pos>
    <Updated>0</Updated>
    <PrintDelay>0</PrintDelay>
    <IsSellOut>9</IsSellOut>
    <IsDelete>0</IsDelete>
    <CalcID>5</CalcID>
    <PackFoodID>0</PackFoodID>
    <SalerPrice>0</SalerPrice>
    <IsPackFood>0</IsPackFood>
    <BarCode>6031</BarCode>
    <Py>agsxhnr </Py>
    <ClickCount>0</ClickCount>
    <InteGralCost>0</InteGralCost>
    <InteGralCost2>0</InteGralCost2>
    <TimePriceStyle>0</TimePriceStyle>
    <Stock>99999</Stock>
    <Name2></Name2>
    <IsInpQuantity>0</IsInpQuantity>
    <CalcLabel></CalcLabel>
    <OrderTimeOut>0</OrderTimeOut>
    <CancelTimeOut>0</CancelTimeOut>
    <IsSpecials></IsSpecials>
    <Unit2></Unit2>
    </item>
    </DataSets>