我有一些留言: SomeMessage.kt
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "inputXml", namespace = "http://some.namespace.com")
data class SomeMessage(
@field:XmlJavaTypeAdapter(CdataAdapter::class)
@field:XmlElement(name = "element")
var element: Element? = null
) : Serializable
CdataAdapter.kt :
@Throws(Exception::class)
override fun unmarshal(v: String?): Any? {
val jaxB = JAXBContext.newInstance(Element::class.java)
val appUnMarshaller = jaxB.createUnmarshaller()
val reader = StringReader(v)
return appUnMarshaller.unmarshal(reader) as Element
}
@Throws(Exception::class)
override fun marshal(a: Any?): String? {
val jaxB = JAXBContext.newInstance(Element::class.java)
val appMarshaller = jaxB.createMarshaller()
val stringWriter = StringWriter()
appMarshaller.marshal(a, stringWriter)
return "<![CDATA[$stringWriter]]>";
}
}
我需要将我的元素放在CDATA中,但是我得到了:
```xml
<ns2:inputXml xmlns:ns2="http://some.namespace.com">
<element><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?><element attrs... /></Element>]]></element>
</ns2:inputXml>
我需要什么
<ns2:inputXml xmlns:ns2="http://some.namespace.com">
<![CDATA[;<element attrs... />]]>
</ns2:inputXml>
我如何在不更改模型的情况下使用jaxb做到这一点?