0

i want that during marshelling special character should escape, is there any way to do this?

alt="<i><b> image alt</b></i>"

this is saved as

&lt;b>&lt;i>image alt&lt;/b>&lt;/i>

i want to save value as it is

ravitech
  • 59
  • 1
  • 8

2 Answers2

1

If you store something as XML, you HAVE to escape that signs. Otherwise you XML will become invalid:

<xml>text</xml>

if test == </xml> the XML will be clearly invalid:

<xml></xml></xml>

This must be:

<xml>&lt;/xml></xml>

If you unmarshall it, it should become the correct value again.

You may also use CDATA

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • I encountered the same problem and unmarshalling did not convert < back to <. However hint on CDATA was helpful, thanks. – uthomas Mar 07 '13 at 07:57
1

I thought I share my experience, because answers I found weren't quit comprehensive (and I'm still not pretty sure if this is the most professional solution out there).

In our project we use maven-jibx-plugin to generate POJOs from XSDs (in two runs as usual: 1. *.xsd->binding.xml, then 2. binding.xml-> *.java).

Based on documentation of value node and Dennis Sosnoski's answer on jibx mailing list I added xml-maven-plugin to our project build process. I use it to apply an XSL file on generated binding.xml before POJO generation. The point is to change value of style attribute on appropriate value node from text to cdata.

So far it seams it solved my encoding issue and now I can return to client xmls like:

<Description><![CDATA[<strong>Valuable content goes here</strong>...<br />]]></Description>

Hope this makes someones life easier. :)

uthomas
  • 677
  • 2
  • 9
  • 17