- Java code
while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if (XmlConstants.RESULT.equals(parser.getName())) { if ("0".equals(parser.getAttributeValue(null, XmlConstants.STATUS))) { return list; } } if (FriendList.MEMBER.equals(parser.getName())) { FriendInfo friend = new FriendInfo(); friend.setUserid(parser.nextText());//下面出现问题,parser.getAttributeValu抛出异常Indexoutofbound异常, 说不是开始tag friend.setName(parser.getAttributeValue(null, FriendList.NAME)); friend.setRelation(parser.getAttributeValue(null, FriendList.RELATION)); friend.setTelno(parser.getAttributeValue(null, FriendList.TELNO)); friend.setBirthday(parser.getAttributeValue(null, FriendList.BIRTHDAY)); list.add(friend); } break; case XmlPullParser.END_TAG: break; } type = parser.next(); } is.close(); return list;
------解决方案--------------------
发一段我写的SAX解析吧
public class NewsHandler extends DefaultHandler{
private boolean in_item=false;
private List<News> li;
private News news;
private String title="";
private StringBuffer buf=new StringBuffer();
public List<News> getParsedData()
{
return li;
}
public String getRssTitle()
{
return title;
}
public void startDocument() throws SAXException
{
li= new ArrayList<News> ();
}
public void endDocument() throws SAXException
{
}
public void startElement(String namespaceURI,String localName,String qName,Attributes atts) throws SAXException
{//遇到开始标签时,判断是否在每个Item项中
if(localName.equals("item"))
{
this.in_item=true;
news=new News();
}
}
public void endElement(String namespaceURI,String localName,String qName) throws SAXException
{//遇到返回标签时将变量存放在相应的变量中
if(localName.equals("item"))
{
this.in_item=false;
li.add(news);
}
else if(localName.equals("title"))
{
if(this.in_item)
{
news.setTitle(buf.toString().trim());
}
}
else if(localName.equals("link"))
{
if(this.in_item)
{
news.setLink(buf.toString().trim());
}
}
else if(localName.equals("description"))
{
if(in_item)
{
news.setDesc(buf.toString().trim());
}
}
else if(localName.equals("pubDate"))
{
if(in_item)
{
news.setDate(buf.toString().trim());
}
}
buf.setLength(0);
}
public void characters(char ch[],int start,int length)
{
if(this.in_item)
{
buf.append(ch,start,length);
}
}
}
------解决方案--------------------
xml文件 根式是什么样的?
都一起发出来 就知道了 pull和 sax都一样 自己 仔细店检查 越界 无非就是 你当前的数据 不是 你要抓去的 自己 加个 判断的标志就是了
------解决方案--------------------
如果数据格式正确,应该不会发生这种事情,请LZ检查一下数据源是否正确。
------解决方案--------------------