NovaCenturion
NovaCenturion

Reputation: 187

java adding cdata to xml string

I need to add CDATA to xml string for sign it with certificate.

String looks like:

<SignedContent>someparametres</SignedContent>

Result must be like:

<![CDATA[<SignedContent>someparametres</SignedContent>]]>

How can i do this? Pls help

P.S. Xml string has only one row (removed all tabs, all spaces, BOM)

Upvotes: 5

Views: 36130

Answers (3)

Rasheed
Rasheed

Reputation: 1051

This post may be hold but i feel i should respond, this may help someone else.

        JAXBContext context = JAXBContext.newInstance(SignedContent.class);
        Marshaller marshallerObj = context.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        marshallerObj.marshal(signedContentObj, sw);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setExpandEntityReferences(false);
        Document doc = factory.newDocumentBuilder().newDocument();
        doc.createCDATASection(sw.toString()).getData();

You can play around from here...

Upvotes: 9

Jon Skeet
Jon Skeet

Reputation: 1500535

It sounds like you just want:

Node cdata = doc.createCDATASection(text);
parentElement.appendChild(cdata);

Upvotes: 9

ceving
ceving

Reputation: 23824

Use Javas + operator:

"<![CDATA[" + "<SignedContent>someparametres</SignedContent>" + "]]>"

Upvotes: -5

Related Questions