簡體   English   中英

沒有 XmlRootElement 注釋的 JAXB 解組?

[英]JAXB unmarshalling without XmlRootElement annotation?

有什么方法可以取消編組沒有@XmlRootElement 注釋的類? 還是我們有義務輸入注釋?

例如:

public class Customer {

    private String name;
    private int age;
    private int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }

}

並讓正確注釋的類的解組代碼如下:

try {

        File file = new File("C:\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
        System.out.println(customer);

      } catch (JAXBException e) {
        e.printStackTrace();
      }

省略細節。

以下代碼用於編組和解組@XmlRootElement

public static void main(String[] args) {

        try {

            StringWriter stringWriter = new StringWriter();

            Customer c = new Customer();
            c.setAge(1);
            c.setName("name");

            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.marshal(new JAXBElement<Customer>( new QName("", "Customer"), Customer.class, null, c), stringWriter);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            InputStream is = new ByteArrayInputStream(stringWriter.toString().getBytes());
            JAXBElement<Customer> customer = (JAXBElement<Customer>) jaxbUnmarshaller.unmarshal(new StreamSource(is),Customer.class);

            c = customer.getValue();

          } catch (JAXBException e) {
            e.printStackTrace();
          }

}

僅當您在 Customer 類上添加@XmlAccessorType(XmlAccessType.PROPERTY)或將所有屬性@XmlAccessorType(XmlAccessType.PROPERTY)以上代碼才有效。

如果您無法將 XmlRootElement 添加到現有 bean,您還可以創建一個持有者類並使用注釋將其標記為 XmlRootElement。 下面的例子:-

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerHolder 
{
    private Customer cusotmer;

    public Customer getCusotmer() {
        return cusotmer;
}

    public void setCusotmer(Customer cusotmer) {
        this.cusotmer = cusotmer;
    }
}

我使用通用解決方案如下:

public static <T> String objectToXmlStringNoRoot(T obj, Class<T> objClass, final String localPart) throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(objClass);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // To format XML
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    //If we DO NOT have JAXB annotated class
    JAXBElement<T> jaxbElement = new JAXBElement<>(new QName("", localPart), objClass, obj);

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(jaxbElement, sw);

    return sw.toString();
}

暫無
暫無

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

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