This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Marshalling Cdata using Jaxb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Marshalling Cdata using Jaxb" Watch "Marshalling Cdata using Jaxb" New topic
Author

Marshalling Cdata using Jaxb

Ameya Pandit
Greenhorn

Joined: Jun 26, 2012
Posts: 2
I am new to JAXB and for my particular requirement, I want to marshal CDATA with JAXB marshaller.
Please suggest a way to do this.

Thanks
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
To do that

[1] First, you can use the overload of Marshaller marshal method to marshal to SAXResult instance. In the SAXResult instance, you supply the fully implemented ContentHandler with the functionality of which being to produce an xml document serialized output faithfully reproduces what marshalling results. That ContentHandler say XTSHandler or whatever named. In this Handler, it may be provisioned with the escaping on angle brackets and ampersand. In that case, if the marshalling output does contain embedded xml into string, it would indeed produce undesired output, semantically incorrect.

[1.1] The easier approach would be to develop the characters method to write out <![CDATA[ and ]]> to wrap around the characters stream when it detects the existence of angle brackets and ampersand in the character stream. In fact, this would be done. The way to detect the tell-tale characters can be done with a simple regex Pattern (see also in [2]).

[2] The harder way and more versatile way to further extension is to subclass the XTSHandler, say, by further implementing LexicalHandler interface. It is this that I want to show the layout. I suppose simpler case to writing out with System.out. If you use a writer, that is a bit more involved but not too much involved.

[2.1] Say such handler be XTSHandler_CDATA. It could consist of these elements.

[2.2] This should be working amid still a lot of space to develop into sophistication. But I prefer commensurate complexity to match the complexity of the problem to handle. The marshaller then marshal the object to the SAXResult instance with the ContentHandler being XDTHandler_CDATA (choose a better name, I just coin it like this for the layout in this illustration.)

[3] There sure are other approaches as well such as more beefed up implementation of jaxb existing in the Eclipse endorsed projects. Another more elementary way I can conceive off-hand as well is to piping the Result to a transformation xslt custom-made with specific element(s) being output as CDATA Section. But the latter is of more restricted scope of usefulness as it needs to know beforehand which elements' text content should be output as CDATA...

Those are the general ideas.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marshalling Cdata using Jaxb
 
Similar Threads
JAXB and CDATA blocks -> bug?
Parsing CDATA using JAXB.
JAXB: Creating a message with a CDATA section
Spring WS JAXB CDATA issue
Soap turning non-ascii chars to garbage