Android异常总结---W/System.err(661): java.lang.IllegalStateException: End of document.
5.03-22 21:42:10.091: W/System.err(661): java.lang.IllegalStateException: End of document.
------------------------------------------------------------------------------------
a.
先看下这个xml文件的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<videonews>
<news id="35">
<title>喜洋洋喜洋洋</title>
<timelength>90</timelength>
</news>
<news id="20">
<title>李德伟大侠</title>
<timelength>15</timelength>
</news>
<news id="50">
<title>马长海和莉莉</title>
<timelength>16</timelength>
</news>
</videonews>
------------------------------------------------------------------------------------------
报了这个错误,这个地方是由于多加了一个:
private static List<News> parseXML(InputStream inStream)throws Exception
{
List<News> newses=new ArrayList<News>();
News news=null;
XmlPullParser parser=Xml.newPullParser();
parser.setInput(inStream, "UTF-8");
int event=parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
//event=parser.next();//原因是这个地方原来没有被注释掉,这样的话,他会一个节点一个节点的读那么第一个节点读出来的就是:<videonews>这样的话,下面的代码就
//没法执行了,
//所以就报了下面的错误:private static List<News> parseXML(InputStream inStream)throws Exception
{
List<News> newses=new ArrayList<News>();
News news=null;
XmlPullParser parser=Xml.newPullParser();
parser.setInput(inStream, "UTF-8");
int event=parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
//event=parser.next();
switch (event)
{
case XmlPullParser.START_TAG://如果不用event=parser.next(),而直接判断的话,那么解析是从<news 开始的,这样才是正确的.
if("news".equals(parser.getName())){
int id= new Integer(parser.getAttributeValue(0));
news=new News();
news.setId(id);
}else if("title".equals(parser.getName())){
news.setTitle(parser.nextText());
}else if("timelength".equals(parser.getName())){
news.setTimelength(new Integer(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("news".equals(parser.getName())){
newses.add(news);
news=null;
}
break;
}
event=parser.next();
}
return newses;
}
}
switch (event)
{
case XmlPullParser.START_TAG:
if("news".equals(parser.getName())){
int id= new Integer(parser.getAttributeValue(0));
news=new News();
news.setId(id);
}else if("title".equals(parser.getName())){
news.setTitle(parser.nextText());
}else if("timelength".equals(parser.getName())){
news.setTimelength(new Integer(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("news".equals(parser.getName())){
newses.add(news);
news=null;
}
break;
}
event=parser.next();
}
return newses;
}