Careful with the XMLStreamReader#getText()!

Igor Spasić
Igor Spasić
Less Than a Minute Read

Let's read the tags content in following XML snippet:

<tag>foo&amp;boo</tag>

with the single call to (supposing everything is initialized correctly up to this point):

xmlStreamReader.getText()

We expect the string 'foo&boo' as result, right?

No! The return value is (or may be) just 'foo'. XMLStreamReader#getText() is allowed to break the text content into several chunks! For above example, we have 3 xml events (of type CHARACTER) sent one after another: foo, &, boo.

Use getText() in a loop to concatenate chunks and retrieve the whole text content. Or use getElementText() :)
Page Comments
If the property javax.xml.stream.isCoalescing is set to true element content must be coalesced and only one CHARACTERS event must be returned for contiguous element content or CDATA Sections.
- Hello everybody!
- how get id of jsf control in liferay when used <h:form>
- Thanks so much!
Interesting .. there are probably some kind memory/performance issue behind this kind of "getText()" design.

The Technicalities of Static Sites

David Oliveira Montenegro
View Blog >

Customization is Dead! Long Live Customization!

David H Nebinger
View Blog >
customization banner image.png

Public Bug Bounty Program

David H Nebinger
View Blog >
bugbounty.png
Showing 1 to 3 of 3 entries.