Question

I am constructing an XML DOM Document with a SAX parser. I have written methods to handle the startCDATA and endCDATA methods and in the endCDATA method I construct a new CDATA section like this:

public void onEndCData() {
    xmlStructure.cData = false;
    Document document = xmlStructure.xmlResult.document;
    Element element = (Element) xmlStructure.xmlResult.stack.peek();
    CDATASection section = document.createCDATASection(xmlStructure.stack.peek().characters);
    element.appendChild(section);
}

When I serialize this to an XML file I use the following line to configure the transformer:

transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "con:setting");

Never the less no <![CDATA[ tags appear in my XML file and instead all backets are escaped to &gt; and &lt;, this is no problem for other tools but it is a problem for humans who need to read the file as well. I am positive that the "con:setting" tag is the right one. So is there maybe a problem with the namespace prefix?

Also this question indicates that it is not possible to omit the CDATA_SECTION_ELEMENTS property and generally serialize all CDATA nodes without escaping the data. Is that information correct, or are there maybe other methods that the author of the answer was not aware of?

Update: It seems I had a mistake in my code. When using the document.createCDATASection() function, and then serializing the code with the Transformer it DOES output CDATA tags, even without the use of the CDATA_SECTION_ELEMENTS property in the transformer.

Was it helpful?

Solution

It looks like you have a namespace-aware DOM. The docs say you need to provide the Qualified Name Representation of the element:

private static String qualifiedNameRepresentation(Element e) {
  String ns = e.getNamespaceURI();
  String local = e.getLocalName();
  return (ns == null) ? local : '{' + ns + '}' + local;
}

So the value of the property will be of the form {http://your.conn.namespace}setting.

OTHER TIPS

In this line

transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "con:setting");

try replacing "con:setting" with "{http://con.namespace/}setting"

using the appropriate namespace

Instead of using a no-op Transformer to serialize your DOM tree you could try using the DOM-native "load and save" mechanism, which should preserve the CDATASection nodes from the DOM tree and write them as CDATA sections in the resulting XML.

DOMImplementationLS ls = (DOMImplementationLS)document.getImplementation();
LSOutput output = ls.createLSOutput();
LSSerializer ser = ls.createLSSerializer();
try (FileOutputStream outStream = new FileOutputStream(...)) {
  output.setByteStream(outStream);
  output.setEncoding("UTF-8");
  ser.write(document, output);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top