2015-11-20 15 views
9

Có cách nào chúng tôi có thể hủy kết hợp cho một lớp học không có chú thích @XmlRootElement không? Hay chúng tôi có nghĩa vụ nhập chú thích?JAXB unmarshalling mà không có chú thích XmlRootElement?

ví dụ:

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; 
    } 

} 

và để mã unmarshalling cho lớp chú thích đúng được như:

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(); 
     } 

bỏ đi các chi tiết.

Trả lời

18

mã sau đây được sử dụng để marshall và unmarshall withot @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(); 
      } 

} 

Trên mã số hoạt động chỉ nếu bạn thêm @XmlAccessorType(XmlAccessType.PROPERTY) trên lớp khách hàng, hoặc làm cho tin tất cả các thuộc tính.

+0

trong thay thế cố gắng để làm cho 'private' tất cả các thuộc tính, nếu không có hoặc không có' XmlRootElement', JAXB không làm việc – Xstian

+1

Trong cách mô tả ở trên, bạn sẽ không cần bất kỳ chú thích khác, ngoại trừ ' @ XmlAttribute' và '@ XmlElement' nhưng chúng mô tả đầu ra – Xstian

+0

Có cần thiết để marshall trước khi unmarshalling? –

1

Nếu bạn không thể thêm XmlRootElement vào bean hiện có, bạn cũng có thể tạo một lớp chủ và đánh dấu nó bằng chú thích là XmlRootElement. Ví dụ dưới đây: -

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; 
    } 
} 
Các vấn đề liên quan