Hi,
I'm trying to create an XML structure and then writing to a file and I have problems with CDATA sections.
I use the next function to create a node with a CDATA section:
private static Element creaElementCDATA(String node, String value){
Element elementFillDades = document.createElement(node);
if (value!=null){
elementFillDades.appendChild(document.createCDATASection(value));
}
return elementFillDades;
}
and this one to write the XML file:
public static void writeXmlFile(Document doc, String filename) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT,"yes");
xformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS,"descripcioAssignatura");
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
The document seems to create the CDATA section, but when I check the created file, it adds a tag with no text value(<descripcioAssignatura/> instead of <descripcioAssignatura><<![.....a large description...]]></descripcioAssignatura>). I added the line in bold, but doesn't work.... any idea??