5

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)

3
  • What XML API are you using? Commented Jun 13, 2014 at 11:58
  • Does it really have to be CDATA? could the content simply be encoded for use within xml (i.e. &lt;SignedContent&gt;someparameters&lt;/SignedContent&gt;)? Commented Jun 13, 2014 at 12:07
  • @BrettOkken yes, it must be CDATA Commented Jun 13, 2014 at 14:06

3 Answers 3

9

It sounds like you just want:

Node cdata = doc.createCDATASection(text); parentElement.appendChild(cdata); 
Sign up to request clarification or add additional context in comments.

2 Comments

How can i add cdata to xml where root element is SignedContent. If i add cdata like u wrote, it will be looks like <SignedContent><![CDATA[someparameters]]></SignedContent>.
@bakash_erni: It's not clear what you mean. What you've shown in your question is CDATA whose content is SignedContent. In other words, you should be taking your SignedContent element, converting that to text, creating a CDATASection from that, and adding it to some other node. You can't have an XML document whose root node is itself CDATA.
9

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...

1 Comment

Oops,my response is in JAVA, since the question did not specify any language
-5

Use Javas + operator:

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

5 Comments

Please don't start manipulating XML documents using string operations. Recipe for failure.
@JonSkeet CDATA contains just character data. It is nothing more than a string.
Good luck with that if the text you want to represent happens to include ]]>. I'd expect an XML API to make that error clear immediately, rather than giving you an invalid XML document. Bypassing the XML model and using strings is pretty much always a bad idea.
Indeed. So will your approach highlight that problem immediately, or create an invalid doc? The latter, I believe - whereas by using an XML API, you can get validation as you construct the doc, making it easier to find problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.