2010-05-05 12 views
7

JAXB treets vuoto int XML attributo come 0, che va bene per me, ma ho bisogno di memorizzarlo come null. Sembra che non possa cambiare la classe DataConverterImpl in implementazione personalizzata. Cosa si potrebbe fare se non del tutto?JAXB treets vuoto int XML attributo come 0

<xs:attribute name="Count" type="Integer0to999" use="optional"> 

<xs:simpleType name="Integer0to999"> 
    <xs:annotation> 
     <xs:documentation xml:lang="en">Used for Integer values, from 0 to 999 inclusive</xs:documentation> 
    </xs:annotation> 
    <xs:restriction base="xs:integer"> 
     <xs:minInclusive value="0"/> 
     <xs:maxInclusive value="999"/> 
    </xs:restriction> 
    </xs:simpleType> 

Dopo la compilazione dello schema xjc ho avuto classe con il seguente:

@XmlAttribute(name = "Count") 
    protected Integer passengerCount; 

Durante XML analizzano parseInt() ottenere chiamato da DataConverterImpl.class da Sun (Oracle), che il codice è al di sotto, la vostra neve in corso per ottenere nulla da questo codice:

public static int _parseInt(CharSequence s) { 
     int len = s.length(); 
     int sign = 1; 

     int r = 0; 

     for(int i=0; i<len; i++) { 
      char ch = s.charAt(i); 
      if(WhiteSpaceProcessor.isWhiteSpace(ch)) { 
       // skip whitespace 
      } else 
      if('0'<=ch && ch<='9') { 
       r = r*10 + (ch-'0'); 
      } else 
      if(ch=='-') { 
       sign = -1; 
      } else 
      if(ch=='+') { 
       ; // noop 
      } else 
       throw new NumberFormatException("Not a number: "+s); 
     } 

     return r*sign; 
    } 

risposta

1

You coul d provare a scrivere un XMLAdaptor personalizzato che converte il numero intero in una stringa (o un altro oggetto), quindi lo converte indietro come si ritiene opportuno. Durante l'unmarshalling, immagino che un rapido check-insensitive per "null" sia tutto ciò di cui avresti bisogno.

Inizia da qui: http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html

collegare l'adattatore al vostro campo int e scrivere il corpo.

il convertitore si avvia come segue:

public class MyIntConverter extends XmlAdapter<String,Integer> 
{ 
    @Override 
    public String marshal(Integer value) throws Exception { 

     if (value.intValue() == 0) 
        return null; // Or, "null" depending on what you'd like 

      return value.toString(); 
    } 

    @Override 
    public Integer unmarshal(String storedValue) throws Exception { 

     if (storedValue.equalsIgnoreCase("null")) 
        return 0; 

      return Integer.valueOf(storedValue); 
    } 
} 

Poi, si potrebbe metterlo in uso:

@XmlJavaTypeAdapter(MyIntConverter.class) 
int someNumber; 

Il codice non è testato, ma penso che dovrebbe risolvere il problema.

1

FWIW, attualmente sto trovando che JAXB unmarshals un elemento vuoto a zero (se un campo Integer) ma a null se si tratta di un campo lungo.

Ammettiamo che potrebbe trattarsi di un qualche tipo di problema con i convertitori di tipi di dati personalizzati. Oppure potrebbe essere un bug con la versione JAXB con JDK 6.0 e potresti utilizzare una versione successiva.