2012-04-04 11 views
9

ho il mio modello:moxy JAXB: come escludere gli elementi da smistamento

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class CustomerTest { 

    private Long id; 

    @XmlPath("contact-info/billing-address") 
    private AddressTest billingAddress; 

    @XmlPath("contact-info/shipping-address") 
    private AddressTest shippingAddress; 

    @XmlPath("FileHeader/SchemaVersion/text()") 
    private String schemaVersion; 
} 

E riempio in oggetto come questo:

private void marshallCustomerTest() { 
     try { 
      JAXBContext jc = JAXBContext.newInstance(CustomerTest.class); 

      CustomerTest customer = new CustomerTest(); 
      customer.setId(new Long(10)); 
      customer.setSchemaVersion("3.2"); 

      AddressTest billingAddress = new AddressTest(); 
      billingAddress.setStreet("1 Billing Street"); 
      customer.setBillingAddress(billingAddress); 

      AddressTest shippingAddress = new AddressTest(); 
      shippingAddress.setStreet("2 Shipping Road"); 
      customer.setShippingAddress(shippingAddress); 

      Marshaller m = jc.createMarshaller(); 
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
      m.marshal(customer, System.out); 
     } catch (JAXBException jex) { 
      jex.printStackTrace(); 
      log.error(jex); 
     } 
    } 

Questa produrre la prossima XML:

<customerTest xmlns:fe="http://www.facturae.es/Facturae/2009/v3.2/Facturae" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
    <id>10</id> 
    <contact-info> 
     <billing-address> 
     <street>1 Billing Street</street> 
     </billing-address> 
     <shipping-address> 
     <street>2 Shipping Road</street> 
     </shipping-address> 
    </contact-info> 
    <FileHeader> 
     <SchemaVersion>3.2</SchemaVersion> 
    </FileHeader> 
</customerTest> 

Come si può vedere non esiste l'annotazione @XmlPath per la proprietà 'id', ma è presente anche nell'XML finale. So che posso evitare questo comportamento impostando la proprietà 'id' su null ma voglio sapere se c'è un altro modo. Il punto è che il mio modello reale è molto più grande di questo e dovrei impostare molte proprietà su null.

Qualsiasi aiuto?

Grazie in anticipo.

risposta

15

è possibile contrassegnare la proprietà con @XmlTransient di averlo escluso dalla rappresentazione XML:

@XmlTransient 
private Long id; 

Oppure si può annotare il tipo con @XmlAccessorType(XmlAccessType.NONE) campi in modo che solo annotati/proprietà vengono mappate.

@XmlAccessorType(XmlAccessType.NONE) 
public class CustomerTest { 

Per ulteriori informazioni

+1

Grazie! Sei sempre molto utile. – rocotocloc

Problemi correlati