Grails XML converters - write CDATA

ぃ、小莉子 提交于 2020-01-05 09:28:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!