2 qizhewoniuxuanwu qizhewoniuxuanwu 于 2014.06.19 10:09 提问

在做项目实训,遇到Xml解析的问题

运行时出现这样的问题setInput() must be called first. (position:START_DOCUMENT null@1:1)

<?xml version="1.0" encoding="UTF-8"?>
-<Response>-<Error><ErrorCode>0</ErrorCode></Error>-<Data>-<UserBean><Id>269</Id><Gender>1</Gender><Department>1</Department><Email>1234567@163.com</Email><Age>11</Age></UserBean></Data></Response>

我的代码是:

public class XmlRegist extends XmlGridParser {

 boolean isLoginfo = false;
 boolean isName = false;
 boolean isGender = false;
 boolean isDepart = false;
 boolean isPass = false;
 boolean isEmail = false;
 boolean isAge = false;
public XmlRegist(InputStream is, XmlPullParser parser) throws ParseException {
    super(is, parser);
}

protected void handleExtraElementInEntry(EntryP entry)
        throws XmlPullParserException, IOException, ParseException {
    XmlPullParser parser = getParser();
    if (!(entry instanceof EntryLogin)) {
        throw new IllegalArgumentException("Expected EntryRegist!");
    }
    EntryRegist regist = (EntryRegist) entry;
    int type = parser.getEventType();
    try
    {


        while (type != XmlPullParser.END_DOCUMENT)
        {
            String name = parser.getName();
           switch( type)
           {
           case XmlPullParser.START_DOCUMENT:
               break;


           case XmlPullParser.START_TAG:
           {if(name.equals("Data"))
           {
            if (type == XmlPullParser.START_TAG
                    && name.equals(regist.USERBEAN)) {

            if (type == XmlPullParser.START_TAG && name.equals(regist.LOFINFO))
            {
                isLoginfo = true;
            } else if (type == XmlPullParser.END_TAG&& name.equals(regist.LOFINFO)) {
                isLoginfo = false;
            }
            else if(type==XmlPullParser.START_TAG&&name.equals(regist.NAME))
            {
                isName=true;
            }else if(type==XmlPullParser.END_TAG&&name.equals(regist.NAME))
            {
                isName=false;
            }
            else if(type==XmlPullParser.START_TAG&&name.equals(regist.GENDER))
            {
                isGender=true;
            }else if(type==XmlPullParser.END_TAG&&name.equals(regist.GENDER))
            {
                isGender=false;
            }
            else if(type==XmlPullParser.START_TAG&&name.equals(regist.DEPART))
            {
                isDepart=true;
            }else if(type==XmlPullParser.END_TAG&&name.equals(regist.DEPART))
            {
                isDepart=false;
            }
            else if(type==XmlPullParser.START_TAG&&name.equals(regist.PASS))
            {
                isPass=true;
            }else if(type==XmlPullParser.END_TAG&&name.equals(regist.PASS))
            {
                isPass=false;
            }
            else if(type==XmlPullParser.START_TAG&&name.equals(regist.AGE))
            {
                isAge=true;
            }else if(type==XmlPullParser.END_TAG&&name.equals(regist.AGE))
            {
                isAge=false;
            }


            else if (type == XmlPullParser.TEXT)
            {
                if (isLoginfo)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setUserId(parser.getText());
                    } 
                }
                if (isName)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setUsername(parser.getText());
                    } 
                }
                if (isGender)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setGender(parser.getText());
                    }
                }
                if (isDepart)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setDepart(parser.getText());
                    }
                }
                if (isPass)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setPass(parser.getText());
                    }
                }

                if (isEmail)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setEmail(parser.getText());
                    }
                }
                if (isAge)
                {
                    if (Util.isNull(parser.getText())) {
                        regist.setAge(parser.getText());
                    }
                }


            }

            else if (type == XmlPullParser.END_TAG
                    && name.endsWith(regist.USERBEAN))
            {
                break;
            }
            type = parser.next();
           }

           }
           }
        }
        }
    } catch (XmlPullParserException e) {
        UtilLog.e("Xml XmlLogin-handleExtraElementInEntry(): ",
                e.getMessage());
    }
}

}

应该是解析时出错了,可是都两天了,实在是找不到原因,只能求助于各位大神啦。
在线坐等

2个回答

xiastars
xiastars   2014.09.23 21:32

我也遇到了这样的问题,但是是这样的,同事在SVN中修改了ServerHost,我这边没有更新完全报了这一个错误,修改后解决了

qq_28295425
qq_28295425   2017.05.28 17:39

没有写 xmlPullParser.setInput(new StringReader(xmlData));

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!