Handling XML escape characters (e.g. quotes) using JAXB Marshaller

前端 未结 5 751
無奈伤痛
無奈伤痛 2020-12-01 21:47

I need to serialize an XML java object to a XML file using the JAXB Marshaller (JAXB version 2.2). Now in the xml object, I have a tag which contains String value

5条回答
  •  甜味超标
    2020-12-01 22:48

    You can leverage the CDATA structure. Standard JAXB does not cover this structure. There is an extension in EclipseLink JAXB (MOXy) for this (I'm the tech lead). Check out my answer to a related question:

    • How to generate CDATA block using JAXB?

    It describes the @XmlCDATA annotation in MOXy:

    import javax.xml.bind.annotation.XmlRootElement;
    import org.eclipse.persistence.oxm.annotations.XmlCDATA;
    
    @XmlRootElement(name="c")
    public class Customer {
    
       private String bio;
    
       @XmlCDATA
       public void setBio(String bio) {
          this.bio = bio;
       }
    
       public String getBio() {
          return bio;
       }
    
    }
    

    For more information see:

    • http://bdoughan.blogspot.com/2010/07/cdata-cdata-run-run-data-run.html

提交回复
热议问题