Rectangle 27 1

How to wrap HTML content in CData (Java) for XSLT XML to HTML?


DOMImplementationLS lsImp = (DOMImplementationLS)doc.getImplementation();

        LSSerializer ser = lsImp.createLSSerializer();
        ser.getDomConfig().setParameter("xml-declaration", false);

        String xml = ser.writeToString(frag1);

        System.out.println(xml);

        param.appendChild(doc.createCDATASection(xml));

        System.out.println(ser.writeToString(doc));
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);

        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.parse("sample1.xml");

        DocumentFragment frag1 = doc.createDocumentFragment();

        Element param = (Element)doc.getElementsByTagName("param5").item(0);

        while (param.hasChildNodes())
        {
            frag1.appendChild(param.getFirstChild());
        }
<content>
    <records>
        <record>
            <param1>1</param1>
            <param2>25</param2>
            <param3>34</param3>
            <param4>b</param4>
            <param5><![CDATA[
                <p>this is html that should be wrapped with CData including the p tags.</p>
            ]]></param5>
        </record>
    </records>
</content>
DOMImplementationLS lsImp = (DOMImplementationLS)doc.getImplementation();
LSSerializer
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentFragment;


import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
writeToString

@user3621633 I still don't see how this is going to help you get the HTML output you are looking for. Transforming this result with the XSLT stylesheet you "don't have control over" will produce something very different.

@user3621633, I have edited the answer to show some code snippets that move the child nodes of the element to a DocumentFragment node, serialize that to a String and then create and add a CDATA section to the element.

Here is a simple example, the imports needed are

If you want to create a CDATA section with the markup of DOM nodes then you first need to serialize those nodes which can be done in Java either using a default transformer or the DOM Load/Save API. So I would create a document fragment node and appendChild all child nodes of the param to the document fragment, the serialize the document fragment to a string then you can use your code to create a CDATA section and appendChild it to the param.

Thank you very much! I see that. Maybe I wasn't clear in my question and that's why I was getting questions about using CData and XSLT but this is exactly what I was looking for! Thank you!!

Thank you! This is what I was asking for in the first place. I was planning on writing an answer using the LSSerializer class to serialize the node, then copy the the HTML markup and content and pass it to the setTextContent method on the original node. Do you have any examples you can provide with the default transformer and/or DOM Load/Save API? Thanks, again.

The pay-grade above me said use CData so I'm using CData. That's what they want. It's not as if the XSL will change dramatically but it could be tweaked over time by another party. With the answer above, it works great and as long as the people above me are happy with it, I'm good :)

then the code to read in the document and find the element is as you posted and the DocumentFragment is used to assemble all child nodes removed from the element:

Note
Rectangle 27 1

How to wrap HTML content in CData (Java) for XSLT XML to HTML?


DOMImplementationLS lsImp = (DOMImplementationLS)doc.getImplementation();

        LSSerializer ser = lsImp.createLSSerializer();
        ser.getDomConfig().setParameter("xml-declaration", false);

        String xml = ser.writeToString(frag1);

        System.out.println(xml);

        param.appendChild(doc.createCDATASection(xml));

        System.out.println(ser.writeToString(doc));
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);

        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.parse("sample1.xml");

        DocumentFragment frag1 = doc.createDocumentFragment();

        Element param = (Element)doc.getElementsByTagName("param5").item(0);

        while (param.hasChildNodes())
        {
            frag1.appendChild(param.getFirstChild());
        }
<content>
    <records>
        <record>
            <param1>1</param1>
            <param2>25</param2>
            <param3>34</param3>
            <param4>b</param4>
            <param5><![CDATA[
                <p>this is html that should be wrapped with CData including the p tags.</p>
            ]]></param5>
        </record>
    </records>
</content>
DOMImplementationLS lsImp = (DOMImplementationLS)doc.getImplementation();
LSSerializer
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentFragment;


import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
writeToString

@user3621633 I still don't see how this is going to help you get the HTML output you are looking for. Transforming this result with the XSLT stylesheet you "don't have control over" will produce something very different.

@user3621633, I have edited the answer to show some code snippets that move the child nodes of the element to a DocumentFragment node, serialize that to a String and then create and add a CDATA section to the element.

Here is a simple example, the imports needed are

If you want to create a CDATA section with the markup of DOM nodes then you first need to serialize those nodes which can be done in Java either using a default transformer or the DOM Load/Save API. So I would create a document fragment node and appendChild all child nodes of the param to the document fragment, the serialize the document fragment to a string then you can use your code to create a CDATA section and appendChild it to the param.

Thank you very much! I see that. Maybe I wasn't clear in my question and that's why I was getting questions about using CData and XSLT but this is exactly what I was looking for! Thank you!!

Thank you! This is what I was asking for in the first place. I was planning on writing an answer using the LSSerializer class to serialize the node, then copy the the HTML markup and content and pass it to the setTextContent method on the original node. Do you have any examples you can provide with the default transformer and/or DOM Load/Save API? Thanks, again.

The pay-grade above me said use CData so I'm using CData. That's what they want. It's not as if the XSL will change dramatically but it could be tweaked over time by another party. With the answer above, it works great and as long as the people above me are happy with it, I'm good :)

then the code to read in the document and find the element is as you posted and the DocumentFragment is used to assemble all child nodes removed from the element:

Note