簡體   English   中英

Java JAXB 將元素編組到內部 class 中的對象

[英]Java JAXB Marshalling the elements to the objects in inner class

我嘗試使用JAXB注釋將 JSON 數據轉換為 XML。 我有JSON有點像這樣:

{
   "Employee": {
      "name": "Tanmay",
      "Email": "tanmaypatil@xyz.com",
      "Address": {
         "City": "Florida",
         "State": "Miami"
      }
   }
}

我想使用編組創建 XML,它看起來像這樣:(請注意JSON中不存在的extension標簽,但我需要添加XML

<Company>
    <Employee>
        <FirstName>Tanmay</FirstName>
        <extension>
            <Address>
                <City>Florida</City>
                <State>Miami</State>
            </Address>
        </extension>
    </Employee>
</Company>

我有以下課程:

我的主要 class 實際上會讀取 JSON 數據並將其編組到 XML。 為簡單起見,我使用 Jackson 讀取 JSON 數據,我刪除了該代碼:

public class Main
{
    public static void main(String[] args) {
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jsonParser = jsonFactory.createParser(new File(Main.class.getClassLoader().getResource("InputEPCISEvents.json").toURI()));
        //I am using the JAKSON jsonParser to read the JSON file and automatically map it to the child classes 
        //Ommiting the JACKSON JsonParser code for simplicity purpose
        JAXBContext context = JAXBContext.newInstance(Employee.class);
        Marshaller mar = context.createMarshaller();
        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        mar.marshal(eventInfo, System.out);
        System.out.println("-----------------");
    }
}

以下是我的父 class 將映射到傳入 JSON:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
public class Employee{
    
    private String name;
    private String email;
    private Address address;
    
    //getters and setters are ommited
    //Noargs and allargs constuctor ommited
    
}

以下是我的Address class:

@XmlRootElement(name = "Employee")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Extension", propOrder = { "name","email","city","state"})
private class Address extends Employee{
    
    @XmlElement(name = "extension")
    private Extension extension;
    
    public Extension getExtension() {
        extension.setCity(getCity());
        extension.setState(getState());
        return extension;
    }
    
    public Extension setExtension(Extension extension) {
        this.setCity(extension.getCity());
        this.setState(extension.getState());
        this.extension = extension;
    }
}

以下是我創建的Extension class 作為Extension標簽將不會出現在傳入的 JSON 但我需要在編組期間將其添加到XML中:

@XmlAccessorType(XmlAccessType.FIELD)
public class Extension{
    private String city;
    private String state;
    
    //getters and setters are ommited
    //Noargs and allargs constuctor ommited
}

我想知道如何使用JAXB執行以下操作。 我可以創建一些額外的類或修改現有的 class 但我無法更改傳入的JSONXML ,因為它們是標准格式。

  1. How can I map the address , city , state from JSON to Class object. If the elements are present within the Address.class then it can be mapped automatically but since I want the extension tag in XML I have moved them to my Extension.class hence the JAXB cannot recognize it and its not mapping to objects.

  2. 如何將附加extension標簽添加到將在編組期間創建的XML 我們可以看到JSON不包含extension標簽,但我需要它在XML中。 如果它的集合,那么我可以使用@XmlElementWrapper ,但在我的情況下它不是集合。

我嘗試了很多搜索,但找不到任何對我有幫助的東西。 因此,根據我的理解,我已經完成了上述事情。 任何幫助或建議將不勝感激。

我認為您必須在 JAXBContext 中注冊所有使用過的類

JAXBContext context = JAXBContext.newInstance(Employee.class, Extension.class, ...)

或者,您也可以傳遞所有類的“根” package:

JAXBContext context = JAXBContext.newInstance("com.demo.beans")

最后,我能夠使用Moxy @XmlPath做到這一點。 如果您使用的是Moxy ,那么您可以使用@XmlPath並添加額外的標簽,而不必創建任何額外的類。

例如,在這種情況下,我不必創建Extension.class ,但我仍然可以將extension標簽添加到我的 XML。 Moxy 是基於 JAXB 的實現。

我花了很多時間試圖弄清楚很多事情。 因此,發布相同的內容可能對將來的某人有所幫助。

  1. 您可以從這里找到如何配置Moxy 不要擔心它非常簡單。

  2. 您可以從這里找到如何使用@XmlPath

go 只需幾個非常簡單的步驟,您就可以在 XML 中獲得包裝標簽,即使是非收藏品。 此外,無需創建多個附加類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM