JAXB - marshal java object with XML string field

风流意气都作罢 提交于 2019-12-23 01:25:13

问题


suppose i have an object with String property that has an XML string. like:

 class myObject {
    String xml;

    @XmlElement(name = "xml", type = String.class)
    public String getXml() {
        return xml;
    }

    public void setXml(String xml) {
        this.xml = xml;
    }
}

i set an XML String to this property - such as

 myObject.setXml("<xml>bbb</xml>");

now i want to marshal it using JAXB and i get:

<xml>&lt;xml&gt;bbb&lt;/xml&gt;</xml>

where i want to get

<xml>bbb</xml>

how can i do it?

EDIT: the problem is that String xml, stores a well formatted XML as a string. so I want this string to be marshaled without escaping the XML characters.


回答1:


If you want to store an XML fragment as a String in your Java model, then you can use the @XmlAnyElement annotation with a DomHandler specified to achieve this.

Examples on Stack Overflow

  • JAXB use String as it is
  • How to marshall a string using JAXB that sometimes contains XML content and sometimes does not?
  • Using JAXB to extract inner text of XML element


来源:https://stackoverflow.com/questions/14777444/jaxb-marshal-java-object-with-xml-string-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!