How to output a CDATA section from a Sax XmlHandler

会有一股神秘感。 提交于 2019-12-01 06:31:44
Josh

It is getting escaped because the handler.characters function is designed to escape and the <![CDATA[ part isn't considered part of the value.

You need to use the newly exposed methods in DefaultHandler2 or use the TransformerHandler approach where you can set the output key CDATA_SECTION_ELEMENTS, which takes a whitespace delimited list of tag names that should output sub text sections enclosed in CDATA.

StreamResult streamResult = new StreamResult(out);
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "column");
hd.setResult(streamResult);
hd.startDocument();
hd.startElement("","","column",atts);
hd.characters(asdf,0, asdf.length());
hd.endElement("","","column");
hd.endDocument();
Dani

You should use startCDATA() and endCData() as delimiters, i.e.

xmlHandler.startElement(uri, lname, "column", attributes);
xmlHandler.startCDATA();
String chars = rs.getString(i);
xmlHandler.characters(chars.toCharArray(), 0, chars.length());
xmlHandler.endCDATA();
xmlHandler.endElement(uri, lname, "column");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!