2011-05-17 5 views
2

Je veux m'assurer qu'un contenu d'élément xml est unmarshalled en majuscules sur mon objet.jaxb, setter d'élément unique pour les collections

public class SZM { 

    String field01; 
    @XmlElement(name="field01") 
    public void setField01(String value) {this.field01 = value.toUpperCase();} 
    public String getField01() {return field01;} 

mais comment faire la même chose pour chaque article d'une collection? Je veux que toute valeur lue du xml soit mise en majuscule.

@XmlElement 
ArrayList<String>collection01; 

Merci à l'avance, Agostino

toute la classe, juste au cas où:

package test.jaxb; 

import java.util.ArrayList; 
import javax.xml.bind.annotation.*; 

@XmlRootElement 
public class SZM { 
    String field01; 
    @XmlElement(name="field01") 
    public void setField01(String value) {this.field01 = value.toUpperCase();} 
    public String getField01() {return field01;} 

    @XmlElement 
    ArrayList<String>collection01; 

} 

Répondre

1

Vous pouvez utiliser un XMLAdapter pour manipuler les valeurs de chaîne:

StringCaseAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter; 

public class StringCaseAdapter extends XmlAdapter<String, String> { 

    @Override 
    public String unmarshal(String v) throws Exception { 
     return v.toUpperCase(); 
    } 

    @Override 
    public String marshal(String v) throws Exception { 
     return v.toLowerCase(); 
    } 

} 

SZM

Vous faites référence à la XMLAdapter comme:

package test.jaxb; 

import java.util.ArrayList; 
import javax.xml.bind.annotation.*; 

@XmlRootElement 
public class SZM { 
    String field01; 

    @XmlElement(name="field01") 
    @XmlJavaTypeAdapter(StringCaseAdapter.class) 
    public void setField01(String value) {this.field01 = value;} 
    public String getField01() {return field01;} 

    @XmlElement 
    @XmlJavaTypeAdapter(StringCaseAdapter.class) 
    ArrayList<String>collection01; 

} 

input.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<szm> 
    <collection01>def</collection01> 
    <collection01>ghi</collection01> 
    <field01>abc</field01> 
</szm> 

Démo

import java.io.File; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(SZM.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     SZM szm = (SZM) unmarshaller.unmarshal(new File("input.xml")); 

     System.out.println(szm.getField01()); 
     for(String item : szm.collection01) { 
      System.out.println(item); 
     } 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(szm, System.out); 
    } 

} 

sortie

ABC 
DEF 
GHI 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<szm> 
    <collection01>def</collection01> 
    <collection01>ghi</collection01> 
    <field01>abc</field01> 
</szm> 
Questions connexes