如何在CDATA中的jaxb中包装标签?

时间:2019-05-28 17:49:58

标签: java xml kotlin jaxb

我有一些留言: 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>&lt;![CDATA[&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;element attrs... /&gt;&lt;/Element&gt;]]&gt;</element>
    </ns2:inputXml>

我需要什么

        <ns2:inputXml xmlns:ns2="http://some.namespace.com">
            &lt;![CDATA[;&lt;element attrs... /&gt;]]&gt;
        </ns2:inputXml>

我如何在不更改模型的情况下使用jaxb做到这一点?

0 个答案:

没有答案