Reading escape characters with XMLStreamReader

Hi I am having a problem reading escape characters inside xml using XMLStreamReader .

For example, I have this element:

<a>foo&amp;bar</a>

and when I read the value, everything after is &amp;truncated, and the value I get is "foo"

Any ideas how this could be fixed?

+3
source share
2 answers

I am not sure what the problem is: my test gives the expected results.

Launch

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(
     new StringReader("<tag>foo&amp;bar</tag>"));
PrintWriter pw = new PrintWriter(System.out, true);
while (reader.hasNext())
{
    reader.next();
    pw.print(reader.getEventType());
    if (reader.hasText())
        pw.append(' ').append(reader.getText());
    pw.println();
}

Gives out

1
4 foo
4 &
4 bar
2
8

In JDK 1.6.0.11 - pretty old, I know. I will update and send messages if the results are different.

, XMLStreamReader ( !) , - 4 (4 = CHARACTERS) , 3 .

+3

XMLStreamReader , javax.xml.stream.isCoalescing, XMLStreamReader # next() documentation

XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty("javax.xml.stream.isCoalescing", true);  // decode entities into one string
XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(stringReader);
+12

Source: https://habr.com/ru/post/1747743/


All Articles