简体   繁体   中英

XmlPullParser name expected exception

I try to read a XML file created by my app using this code:

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();
}

but at first, when eventType is START_DOCUMENT, next() function throws exception: org.xmlpull.v1.XmlPullParserException: name expected (position:START_TAG @2:2 in java.io.StringReader@41084cc8)

Do you have any idea why?

Here is my XML file:

<?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>

Make your XML well-formed by giving it a single root element and making element names start with letters, not numbers:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM