It can e.g. make a difference if the text content of an element is a mix of plain &-encoded text, and CDATA-encoded text.
Demo
public static void main(String[] args) throws Exception {
test(false);
test(true);
}
static void test(boolean coalesce) throws Exception {
System.out.println("IS_COALESCING = " + coalesce + ":");
String xml = "<Root>abc<![CDATA[def]]>ghi</Root>";
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, coalesce);
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new StringReader(xml));
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isCharacters())
System.out.println(" "" + event.asCharacters().getData() + """);
}
}
Output
IS_COALESCING = false:
"abc"
"def"
"ghi"
IS_COALESCING = true:
"abcdefghi"
If you parsed into DOM, the <Root>
element would have 3 Node
children:
Text
where getData()
returns "abc"
CDATASection
where getData()
returns "def"
Text
where getData()
returns "ghi"
The XMLInputFactory
property works the same as the DocumentBuilderFactory.setCoalescing(boolean coalescing)
method:
Specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…