简体   繁体   中英

JAXB - marshal java object with XML string field

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM