无法将类型[...]编组为元素,因为它缺少@XmlRootElement注释

时间:2017-12-13 14:30:05

标签: java jaxb

令人难以置信的有关此主题的问题有多少。尽管如此,他们都没有帮助我,因为我所理解的是这是一个常见错误,我相信我的代码是正确的,因为它与我的同事通过我的另一个项目完全相同。

但是......我有这个错误

unable to marshal type "modules.CollaborationInfo" as an element because it is missing an @XmlRootElement annotation

鉴于此类CollaborationInfo:

package modules;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CollaborationInfo", propOrder = {
    "AgreementRef",
    "ConversationId"
})


public class CollaborationInfo {
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name="AgreementRef")

    public static class AgreementRef {
        @XmlAttribute
        private String pmode;

        public String getPmode() {
            return pmode;
        }
        public void setPmode(String pmode) {
            this.pmode = pmode;
        }
    }

    @XmlElementRef(name = "AgreementRef")
    protected AgreementRef AgreementRef = new AgreementRef();

    @XmlElement(name="ConversationId")
    protected String ConversationId;


    public String getPmode() {
        return AgreementRef.getPmode();
    }

    public void setPmode(String value) {
        this.AgreementRef.setPmode(value);
    }


    public String getConversationId() {
        return ConversationId;
    }
    public void setConversationId(String value) {
        this.ConversationId = value;
    }


}

并作为main():

public static void main(String[] args) throws Exception{
    JAXBContext contextObj = JAXBContext.newInstance(CollaborationInfo.class);
    Marshaller marshallerObj = contextObj.createMarshaller();
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    CollaborationInfo CI = new CollaborationInfo();
    CI.setPmode("String1");
    CI.setConversationId("String2");

    marshallerObj.marshal(CI, new FileOutputStream("C:\\OUT.xml"));
}

我应该可以输出(OUT.xml):

<?xml version="1.0" encoding="UTF-8"?>
    <CollaborationInfo>
        <AgreementRef pmode="String1"/>
        <ConversationId>String2</ConversationId>
    </CollaborationInfo>

但我不能。 有人能告诉我哪里错了吗?

(当然真正的XML更长更复杂,但可能如果我能够让这部分工作,我可以继续其余的工作)

2 个答案:

答案 0 :(得分:1)

如错误所示,您需要在类@XmlRootElement前面添加注释CollaborationInfo。您的@XmlRootElement适用于静态类AgreementRef

package modules;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CollaborationInfo", propOrder = {
    "AgreementRef",
    "ConversationId"
})
public class CollaborationInfo {

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class AgreementRef {
        @XmlAttribute
        private String pmode;

        public String getPmode() {
            return pmode;
        }
        public void setPmode(String pmode) {
            this.pmode = pmode;
        }
    }

    @XmlElement(name = "AgreementRef")
    protected AgreementRef AgreementRef = new AgreementRef();

    @XmlElement(name="ConversationId")
    protected String ConversationId;


    public String getPmode() {
        return AgreementRef.getPmode();
    }

    public void setPmode(String value) {
        this.AgreementRef.setPmode(value);
    }


    public String getConversationId() {
        return ConversationId;
    }
    public void setConversationId(String value) {
        this.ConversationId = value;
    }
}

答案 1 :(得分:0)

这是旧的,但以防万一有人遇到它,在这里回答: https://stackoverflow.com/a/59249216/5835746

基本上,您不应直接使用自动生成的类,而应使用 ObjectFactory 中的方法(也是由插件自动生成的)来创建这些类的实例。

编辑自动生成的类,添加@XmlRootElement 注释可能不是一个好主意。