簡體   English   中英

將Java對象轉換為XML

[英]Convert Java object to XML

我試圖將Java庫中的Java對象轉換為XML文件。 但是,我遇到了這個問題:

A a = new A();

// initializing for a

JAXBContext jc = JAXBContext.newInstance("libraryA.A");

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(a, System.out);

然后我得到了這個例外:

javax.xml.bind.JAXBException: "libraryA.a" doesnt contain ObjectFactory.class or jaxb.index
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:186)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:128)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:290)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)

如果我改變: JAXBContext jc = JAXBContext.newInstance("libraryA.a");

至:

JAXBContext jc = JAXBContext.newInstance(libraryA.A.class);

然后我有另一個例外:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions

library.A is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at library.A

library.A does not have a no-arg default constructor.
    this problem is related 

to the following location:
    at library.A

背景資料(來自相關問題

根據您對上一個問題的回答所做的評論,域模型已經與JAXB一起使用。 讓客戶端和服務器通過XML進行通信的最簡單方法是利用兩端已注釋的模型。

我剛檢查了客戶端的源代碼。 在此過程中,我們需要使用以下命令將從java對象生成的xml文件轉換回xml文件:javax.xml.bind.JAXBContext和javax.xml.bind.Marshaller。 所以我的問題是可以將xml文件讀回到相同的java對象嗎? 然后我們可以使用java對象進一步完成。 提前致謝!


UPDATE

您的問題似乎是由於具有通過具有支持實現類的接口定義的域模型。 下面我將演示如何使用JAXB實現(Metro,MOXy,JaxMe等)來處理這個問題。

演示代碼

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(CustomerImpl.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml);

        Address address = customer.getAddress();
        System.out.println(address.getStreet());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

}

接口模型

以下接口表示我們的域模型。 不利用這些接口來引導JAXBContext。

顧客

public interface Customer {

    public Address getAddress();

    public void setAddress(Address address);

}

地址

public interface Address {

    public String getStreet();

    public void setStreet(String street);

}

實現類

實現類是使用JAXB映射到XML的實現類。

CustomerImpl

請注意,在CustomerImpl類中,我們在address屬性上使用@XmlElement注釋來指定類型是AddressImpl

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="customer")
public class CustomerImpl implements Customer {

    private Address address;

    @XmlElement(type=AddressImpl.class)
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}

AddressImpl

public class AddressImpl implements Address {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

input.xml中

<?xml version="1.0" encoding="UTF-8"?>
<customer>
    <address>
        <street>1 Any Street</street>
    </address>
</customer>

如果您不必使用JAXB,也許您可​​以使用XStream

它對你可以串行到XML或從XML串行的內容幾乎沒有限制,如果你不需要專門的 JAXB,它可能是合適的。

暫無
暫無

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

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