【问题标题】:Convert Object to JAXBElement<Object>将对象转换为 JAXBElement<Object>
【发布时间】:2018-03-08 21:04:48
【问题描述】:

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

【问题讨论】:

标签: java spring spring-boot soap wsdl


【解决方案1】:

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 类,您就拥有了所需的一切。

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2017-07-04
    • 2012-10-21
    • 2021-09-27
    • 1970-01-01
    • 2022-11-01
    • 2011-11-27
    相关资源
    最近更新 更多