问题
Is writing CDATA from GRAILS converters possible? I have a requirement of writing CDATA for some XML elements in a GRAILS app. After running through the grails code in debugger, it looks like the XMLStreamWriter is not accessible. Any recommendations?
回答1:
there's no possibility to add cdata via xml converters. you can use StreamingMarkupBuilder: Generate CDATA inside of tag using StreamingMarkupBuilder
or, have a nice uglyhack, copying '.chars' method from groovy converter definition right in your code, writing chars unescaped:
converter.startNode('property')
converter.attribute('name', propertyName)
converter.startNode('value')
//converter.chars("<![CDATA[" + propertyValue + "]]>")
//as chars in xml converter go escaped, had to rewrite the method here:
converter.checkState();
try {
if (converter.writer.mode == TAG) {
converter.writer.endStartTag();
}
converter.writer.mode = CONTENT;
//we put the data unescaped:
converter.writer.writer.unescaped().write("<![CDATA[" + propertyValue + "]]>");
}
catch (Exception e) {
throw ConverterUtil.resolveConverterException(e);
}
//-----------------
converter.end()
converter.end()
来源:https://stackoverflow.com/questions/33215126/grails-xml-converters-write-cdata