How to output a CDATA section from a Sax XmlHandler

后端 未结 2 1304
孤城傲影
孤城傲影 2021-01-13 20:14

This is a followup question of How to encode characters from Oracle to Xml?

In my environment here I use Java to serialize the result set to xml. I have no access to

2条回答
  •  醉话见心
    2021-01-13 20:45

    It is getting escaped because the handler.characters function is designed to escape and the 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();
    

提交回复
热议问题