簡體   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