2015-11-20 14 views
9

C'è un modo in cui possiamo annullare un marshall per una classe senza l'annotazione @XmlRootElement? O siamo obbligati a inserire l'annotazione?UnMarshalling di JAXB senza annotazione XmlRootElement?

ad esempio:

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

} 

e lasciare che il codice di deserializzazione per la classe correttamente annotati essere come:

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

tralasciando i dettagli.

risposta

18

Dopo codice viene utilizzato per Marshall e 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(); 
      } 

} 

Sopra codice funziona solo se si aggiunge @XmlAccessorType(XmlAccessType.PROPERTY) sulla classe Customer, o fare privati ​​tutti gli attributi.

+0

in prova alternativa per fare 'private' tutti gli attributi, altrimenti con o senza' XmlRootElement', JAXB non funziona – Xstian

+1

nel modo descritto sopra di voi non avrà bisogno di altre annotazioni, tranne ' @ XmlAttribute' e '@ XmlElement' ma descrivono l'output – Xstian

+0

È necessario eseguire il marshalling prima di unmarshalling? –

1

Se non è possibile aggiungere XmlRootElement al bean esistente, è anche possibile creare una classe titolare e contrassegnarla con annotazione come XmlRootElement. Esempio di seguito: -

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