aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to put XML data inside CData Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to put XML data inside CData" Watch "How to put XML data inside CData" New topic
Author

How to put XML data inside CData

Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 22
Hi All,
I have a string which is infact an xml data. I want to put this xml data inside a cdata and then set it in my main xml tree:

Currently, when i set my xml data into my main xml tree, the xml data gives me encoding problem.




I want this xml data (which inside <ns3:Text>) to be set as part of CDATA so that it appears properly.

I have just previewed my question and somehow the xml snippet appears without any encoding problem here on the forum. The xml data inside <ns3:Text> appears with & lt ..& gt..etc

Could someone please suggest any standard java solution with the help of which i can put my xml data inside CData?

Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Exactly how are you creating this XML document?

If you are building a DOM in memory, why not create a CDATASection node as the child of the ns3:Text Element?

If you are writing text, just add the CDATA start and end tags

Bill
Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 22
i just have a control over preparing the xml string and passing it over to another project which embeds it inside the main xml tree.

I have appended the CData to my xml string but still no change: <ns3:Text><![CDATA[<RawProfile><ROWSET type = "AllLinkedParents"><..../RawProfile>]]></ns3:Text>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
You say "encoding problem" - what exactly is the error you get?

I got stuck with a similar horrible design in a client's project several years ago - XML formatted tags mixed with HTML inside a CDATA section is a real pain.

It took a lot of fiddling around to get that to come out right.

Eventually I figured out a sequence of encoding the various magic characters.

Bill
Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 22
my xml inside <text> is not appearing properly. It has those < and > signs instea of < >.

Could you please show me one example snipet using Dom with which i can create an Element (w3c.dom), add my xml string within Cdata to it and then set this Element to the main xml tree. There is a interface to the main xml project which can take my newly created Element.

Thanks.
Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 22
my xml inside <text> is not appearing properly. It has those < and > signs instea of < >.

Could you please show me one example snipet using Dom with which i can create an Element (w3c.dom), add my xml string within Cdata to it and then set this Element to the main xml tree. There is a interface to the main xml project which can take my newly created Element.

Thanks.

I have done somethign like this:



But i get the exception on my jboss 5.1 server..org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
my xml inside <text> is not appearing properly. It has those < and > signs instea of < >.


"Appearing" is the key to untangling your problem. How are you displaying the results?

As you see, the JavaRanch forum formatter and your browser have modified the appearance of your original text.

Personally, I use a programmer's editor that can show plain text and the hex values of characters when needed to help.

Bill
Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 22
I have no idea how to make it appear in a correct way. Have alook at here: Problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to put XML data inside CData