Search code examples
xmlparsingstax

Current state not START_ELEMENT


I have this XML

<request>
<accountid>123</accountid>
<accountid>1234</accountid>
</request>

I am using STAX parser to read this

XMLEventReader eventReader = inputFactory.createXMLEventReader(reader);
if (startElement.getName().toString().equals("accountid"))
{
item.account = eventReader.getElementText();
}

But i am getting

com.ctc.wstx.exc.WstxParsingException: Current state not START_ELEMENT
 at [row,col {unknown-source}]: [2,31]
    at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:630)
    at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:461)
    at com.ctc.wstx.sr.BasicStreamReader.getElementText(BasicStreamReader.java:661)
    at com.ctc.wstx.evt.WstxEventReader.getElementText(WstxEventReader.java:118)
    at Pavan.main(Pavan.java:49)

Solution

  • I encountered this error and it appears that this is a bug in the Wstx implmenetation. I happened to hit a jira on the same issue but the ticket was closed.

    http://jira.codehaus.org/browse/WSTX-179

    If I dont use wstx, I dont see this issue. For now I used sjsxp.jar instead of wstx and it started working. Not sure if it is some bug within Wstx or a validation feature that is causing the xml to fail.

    I am a glassfish user and I had to override the glassfish libraries for XML parsing.