How to marshall a string using JAXB that sometimes contains XML content and sometimes does not?

后端 未结 3 1820
小蘑菇
小蘑菇 2020-12-04 02:47

Consider this example -

I have a class called Report that has a field of type Message. The Message class has a field called \"body\" which is a string. \"body\" can

3条回答
  •  猫巷女王i
    2020-12-04 02:50

    3 different solutions 1), 2) 3), here below :

    1) Following post is a the description of your solution Loresh :

    http://anna-safronova.livejournal.com/2524.html?thread=9180

    This is still missing limitations details.

    • With embeeded html, we need a block

    • JAXB's dependancy

    com.sun.xml.bind.marshaller.CharacterEscapeHandler

    Needs import jaxb-impl for compilation / and may be required for excution, e.g.

    com.sun.xml.bind
    jaxb-impl  
     2.2.4
    
    • Limitation : this solution is Container-specific and may not run because of class-loading policy.

    2) Another similar approach is JDK's rt.jar dependancy

    com.sun.xml.internal.bind.CharacterEscapeHandler

    http://theopentutorials.com/tutorials/java/jaxb/jaxb-marshalling-and-unmarshalling-cdata-block/

    Same limitation / dependends on target JDK, and some tweaks on Eclipse/Maven are necessary (bad alternative / My opinion)

    3) Finally, the best solution was found on another post of Reg Whitton :

    https://stackoverflow.com/a/12637295/560410

    and this is the detailed reciepe :

    http://javacoalface.blogspot.co.uk/2012/09/outputting-cdata-sections-with-jaxb.html

    Worked perfect for me !

提交回复
热议问题