XmlPullParser 名称预期异常
XmlPullParser name expected exception
我尝试读取由我的应用程序使用以下代码创建的 XML 文件:
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(data));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT)
{
switch(eventType)
{
case XmlPullParser.START_TAG:
{
String tagName = xpp.getName();
// do something
break;
}
case XmlPullParser.TEXT:
{
// get text...
break;
}
case XmlPullParser.END_TAG:
{
// do something
break;
}
}
eventType = xpp.next();
}
但起初,当 eventType 为 START_DOCUMENT 时,next() 函数抛出异常: org.xmlpull.v1.XmlPullParserException: 名称预期 (position:START_TAG @2:2 in java.io.StringReader@41084cc8)
你知道为什么吗?
这是我的 XML 文件:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<0>
<createTime>1419453655800</createTime>
<editTime>1419453655800</editTime>
<color>2</color>
<text>ooooo</text>
</0>
<1>
<createTime>1419453586197</createTime>
<editTime>1419453605679</editTime>
<color>1</color>
<text>uuuuuuuuu</text>
</1>
<2>
<createTime>1419453358866</createTime>
<editTime>1419453597124</editTime>
<color>2</color>
<text>yyyyyyyyyy</text>
</2>
通过给它一个根元素并使元素名称以字母而不是数字开头来创建 XML well-formed:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<top>
<e0>
<createTime>1419453655800</createTime>
<editTime>1419453655800</editTime>
<color>2</color>
<text>ooooo</text>
</e0>
<e1>
<createTime>1419453586197</createTime>
<editTime>1419453605679</editTime>
<color>1</color>
<text>uuuuuuuuu</text>
</e1>
<e2>
<createTime>1419453358866</createTime>
<editTime>1419453597124</editTime>
<color>2</color>
<text>yyyyyyyyyy</text>
</e2>
</top>
我尝试读取由我的应用程序使用以下代码创建的 XML 文件:
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(data));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT)
{
switch(eventType)
{
case XmlPullParser.START_TAG:
{
String tagName = xpp.getName();
// do something
break;
}
case XmlPullParser.TEXT:
{
// get text...
break;
}
case XmlPullParser.END_TAG:
{
// do something
break;
}
}
eventType = xpp.next();
}
但起初,当 eventType 为 START_DOCUMENT 时,next() 函数抛出异常: org.xmlpull.v1.XmlPullParserException: 名称预期 (position:START_TAG @2:2 in java.io.StringReader@41084cc8)
你知道为什么吗?
这是我的 XML 文件:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<0>
<createTime>1419453655800</createTime>
<editTime>1419453655800</editTime>
<color>2</color>
<text>ooooo</text>
</0>
<1>
<createTime>1419453586197</createTime>
<editTime>1419453605679</editTime>
<color>1</color>
<text>uuuuuuuuu</text>
</1>
<2>
<createTime>1419453358866</createTime>
<editTime>1419453597124</editTime>
<color>2</color>
<text>yyyyyyyyyy</text>
</2>
通过给它一个根元素并使元素名称以字母而不是数字开头来创建 XML well-formed:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<top>
<e0>
<createTime>1419453655800</createTime>
<editTime>1419453655800</editTime>
<color>2</color>
<text>ooooo</text>
</e0>
<e1>
<createTime>1419453586197</createTime>
<editTime>1419453605679</editTime>
<color>1</color>
<text>uuuuuuuuu</text>
</e1>
<e2>
<createTime>1419453358866</createTime>
<editTime>1419453597124</editTime>
<color>2</color>
<text>yyyyyyyyyy</text>
</e2>
</top>