2012-04-04 14 views
9

mam model:Moxy JAXB: Jak wykluczyć elementy z zestawiania

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

I wypełnić obiekt jak ten:

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

Ten wyprodukować kolejną 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> 

Jak widać, nie ma adnotacji @XmlPath dla właściwości "id", ale jest ona również obecna w ostatecznym kodzie XML. Wiem, że mogę uniknąć tego zachowania, ustawiając właściwość 'id' na wartość null, ale chcę wiedzieć, czy jest inny sposób. Chodzi o to, że mój prawdziwy model jest znacznie większy niż ten i musiałbym ustawić wiele właściwości na zerową.

Każda pomoc?

Z góry dziękuję.

Odpowiedz

15

Można zaznaczyć daną nieruchomość @XmlTransient się on wykluczony z reprezentacji XML:

@XmlTransient 
private Long id; 

Albo można opisywać typ z @XmlAccessorType(XmlAccessType.NONE) tak, że tylko adnotacją pola/właściwości są odwzorowane.

@XmlAccessorType(XmlAccessType.NONE) 
public class CustomerTest { 

Aby uzyskać więcej informacji

+1

Dziękujemy! Zawsze jesteś bardzo pomocny. – rocotocloc