How to infer a CDATA section from Sax XmlHandler

This is the next question How to encode characters from Oracle to Xml?

In my environment here, I use Java to serialize a result set in xml. I do not have access to the output stream itself, only to org.xml.sax.ContentHandler.

When I try to output the characters in the CDATA section:

This happens mainly like this:

xmlHandler.startElement(uri, lname, "column", attributes); String chars = "<![CDATA["+rs.getString(i)+"]]>"; xmlHandler.characters(chars.toCharArray(), 0, chars.length()); xmlHandler.endElement(uri, lname, "column"); 

I get this:

 <column>&lt;![CDATA[33665]]&gt;</column> 

But I want this:

 <column><![CDATA[33665]]></column> 

So how can I get the CDATA partition with Sax ContentHandler?

+4
source share
2 answers

This escapes because the handler.characters function is intended to exit, and the <![CDATA[ part is not considered part of the value.

You need to use the newly opened methods in DefaultHandler2 or use the TransformerHandler approach, where you can set the CDATA_SECTION_ELEMENTS output key, which uses a list of tag names with a space that should output subtext 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(); 
+5
source

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"); 
+3
source

Source: https://habr.com/ru/post/1277347/


All Articles