user191776
user191776

Reputation:

Current Event of an XMLEventReader

From XMLEventReader:

String getElementText()
Reads the content of a text-only element. 
Precondition: the current event is START_ELEMENT.
Postcondition: the current event is the corresponding END_ELEMENT.

What is the "current" event of an XMLEventReader, being referred here?

Is it the event that was returned by the last call for that reader:

a) to nextEvent(), or
b) to peek()?

From the answer of the previous question, how do you interpret this snippet from listing 2 of StAX'ing up XML:

while (reader.hasNext()) {
    XMLEvent event = reader.peek();
    if (event.isStartElement()) {
        StartElement start = event.asStartElement();
        if (ICON.equals(start.getName())) {
            System.out.println(reader.getElementText());
            break;
        }
    }
    reader.nextEvent();
}

I'm encountering a ParseError: parser must be on START_ELEMENT to read next text.

Upvotes: 0

Views: 3481

Answers (1)

Max
Max

Reputation: 1040

Please be aware that .peek() does not read from the Stream. The Documentation for .peek() says:

Check the next XMLEvent without reading it from the stream. Returns null if the stream is at EOF or has no more XMLEvents. A call to peek() will be equal to the next return of next().

So, the position of your Pointer does not change. Therefore, you are probably not on a START_ELEMENT, but most probably at the START_DOCUMENT. Please call .nextEvent() instead of .peek(), that should make it work.

Good luck, Max

Upvotes: 2

Related Questions