将Object转换为JAXBElement <object>

时间:2018-03-08 21:04:49

标签: java spring spring-boot soap wsdl

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

1 个答案:

答案 0 :(得分:0)

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-&gt; JAXB类,即可获得所需的一切。

我希望这有帮助!

相关问题