JAXB - marshal object with XML string property

Suppose I have an object with a String value that has an XML string. as:

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 the XML String to this property - for example,

  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 this?

+2
source share
2 answers

For this you use @XmlAnyElement . However, first you need to convert your XML string into a DOM structure and inject it into your model, and not into a raw XML string.

+2
source

If you want to output pre-built XML (and not just XML serialized as strings, which JAXB gives you correctly), you'd better feed it a DOM Element. Otherwise, JAXB cannot force the XML generator to flush random text without proper escaping.

Where do you get XML? If this occurs as DOM material, it should be relatively easy.

0
source

Source: https://habr.com/ru/post/1400855/


All Articles