繁体   English   中英

将对象转换为JAXBElement <Object>

[英]Convert Object to JAXBElement<Object>

我正在尝试使用一种Web服务,我需要使用元素类型“ JAXBElement”来设置请求的参数。 我有这个特定的对象,但是我不知道如何将我的对象转换为JAXBElement。 我已经搜索了一些相关主题,但是没有找到明确的答案。 我需要做什么来转换该对象?

XMLGenerator.class包含以下代码

public void generateXML() {
    try {
        List<JAXBClass> jaxbClassList= this.jaxbObjectList;
        outputElement.setJAXBObject(jaxbClassList);
        marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "xsd location of the file used to create this xml");
        marshaller.marshal(outputElement,new File("Example.xml"););

    } catch (JAXBException e) {
        e.printStackTrace();
    }

}

JAXBClass.class

 @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "JAXBClass", propOrder = {
    "referenceDate",
    "roles",
    "identifiers",
    "name",
    "address"
   })
public static class JAXBClass{


    @XmlElement(name = "ReferenceDate", required = true)
    protected String referenceDate;


    @XmlElement(name = "Roles", required = true)
    protected RolesType roles;
    @XmlElement(name = "Identifiers", required = true)
    protected IdentifierType identifiers;


    @XmlElement(name = "Name", required = true)
    protected String name;

    @XmlElement(name = "Address", required = true)
    protected CounterPartyList.CounterPartyTO.Address address;

//setter and getters of the above variables
}

用JAXBClass中的变量映射您的输入类,并很好用XML编写它。
在Eclipse中,您不必编写上述类,只需右键单击xsd文件,然后单击generate-> JAXB类 ,您便拥有了所需的一切。

我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM