StAX: Get EntityReferences in XML file

Problem:
The content in a tag does not get returned complete. The return text only has the part before entity reference.

<thetext>
Opening repository resource always open the default text editor and doesn&apos;t
	honor any mapping between resource types and editors.
</thetext>


In the XML snippet above, it contains a ['] inside. The event.asCharacters().getData() does not return all text, but only the part before ['].

if (event.asStartElement().getName().getLocalPart().equals("thetext")) {
	event = eventReader.nextEvent();
					System.out.println("thetext: " + event.asCharacters().getData());
}

The problem is fixed by set one of input factory's properties.

inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);

This requires the parser to replace internal entity references with their replacement text and report them as characters.

Reference: http://download.oracle.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html#IS_COALESCING

Category >> StAX  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. SJ on 2014-11-26

    What a lifesaver! Thanks You!

  2. Arun Selvan on 2015-9-27

    Thanks for the code.. Its very helpful………..

Leave a comment

*