Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem writing XML to a file (CDATA)

843834Oct 25 2006 — edited Oct 30 2006
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??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2006
Added on Oct 25 2006
4 comments
274 views