JAXB元帅jdom.element

时间:2018-10-12 09:39:21

标签: groovy jaxb jdom

我面临一个必须将Groovy对象序列化为xml的问题。

这是我用来序列化的代码:

static <T> Element serializeGenericObjectToXmlTypeElement(T object, boolean removeDeclaration = false) {
        JAXBContext jc = JAXBContext.newInstance(object.getClass())
        Marshaller marshaller = jc.createMarshaller()

        if (removeDeclaration) {
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true)
        }

        Writer writer = new StringWriter()
        marshaller.marshal(object, writer)
        def xmlString = writer.toString()
        def stringReader = new StringReader(xmlString)
        def builder = new SAXBuilder()
        def doc = builder.build(stringReader)
        def elem = doc.getRootElement()
        MyXMLFormatter xmlFormatter = new MyXMLFormatter(elem)
        Element element2 = xmlFormatter.formatXml(elem)
        return element2
    }

在marhsaller.marshal(对象,作家)上失败

我正在发送一个大的嵌套对象,但我认为它在以下对象(其中元素为org.jdom.element,版本1.13)中失败:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RapportIndholdType", propOrder = [
    "any"
])
class RapportIndholdType {

    @XmlAnyElement
    Element any
}

由于我收到以下错误消息:

[com.sun.istack.internal.SAXException2: class org.jdom.Element nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class org.jdom.Element nor any of its super class is known to this context.]

当我查看xml时,生成的最后一部分是这样的:

<ns5:RapportIndhold

挑战在于,任何元素,在任何情况下都可以是任何xml(转换为jdom元素),因此我需要能够编组jdom元素。

我尝试使用@XmlAnyElement(lax = true),但这没用。

有人知道我如何使它工作的解决方案吗?

0 个答案:

没有答案