Serializing Java DOM Document to XML: Add CData Elements

lanoxx

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.

McDowell

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reading CDATA XML in Java

How to add Doctype in XML document using DOM (JAVA)

Serializing supplementary unicode characters into XML documents with Java

How to add an XML namespace (xmlns) when serializing an object to XML

Java XML Serializing, Missing fields in file

Java XML: using DOM with StAX to construct a document

Surround XML elements with CDATA section via XSLT

Ignoring null references when de-serializing a massive XML document

Add array of elements to dom

Javascript serializing CDATA with XMLSerializer

Serializing a XML Document with 3 Levels

How to unmarshal an XML containing a CDATA section within a CDATA in Java?

error Esper XML dom document

serializing single elements of CXML-STP:DOCUMENT

How to match all xml nodes with cdata-section-elements

XML reading with DOM Document in PHP

Serializing key value pairs into xml elements with the key being an attribute

Select and manipulate elements in DOM in XML document in jQuery?

Best Practice for Serializing an XML Document from a Class

No xml Tag with SimpleXML an DOM Document

Java CEF - Is it possible to access DOM document and elements of loaded page?

Serializing to XML file creates invalid XML document (11,12)

Serializing class add prefixes automatically to XML elements

Java for loop to obtain child elements from xml document

How to add <! [CDATA[ tag to exist XML file

Parse response xml and count XML elements in CDATA

Add multiple elements to XML document

Is there a way to access XML elements inside CDATA with Xpath?

XSLT to XML transform | add url and CDATA to tags