Collections jaxb,集合的单项设置器

Collections jaxb,集合的单项设置器,collections,jaxb,getter-setter,Collections,Jaxb,Getter Setter,我想确保在我的对象上,xml元素内容以大写字母取消编组 public class SZM { String field01; @XmlElement(name="field01") public void setField01(String value) {this.field01 = value.toUpperCase();} public String getField01() {return field01;} 但是如何对集合中的每个项目执行相同的操作?

我想确保在我的对象上,xml元素内容以大写字母取消编组

public class SZM {

    String field01;
    @XmlElement(name="field01")
    public void setField01(String value) {this.field01 = value.toUpperCase();}
    public String getField01() {return field01;}
但是如何对集合中的每个项目执行相同的操作?我希望从xml读取的任何值都是大写的

@XmlElement
ArrayList<String>collection01;
@xmlement
ArrayListcollection01;
提前感谢,, 阿戈斯蒂诺

全班同学,以防万一:

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;

}
package test.jaxb;
导入java.util.ArrayList;
导入javax.xml.bind.annotation.*;
@XmlRootElement
公共级SZM{
字符串字段01;
@xmlement(name=“field01”)
public void setField01(字符串值){this.field01=value.toUpperCase();}
公共字符串getField01(){return field01;}
@XmlElement
ArrayListcollection01;
}

您可以使用XmlAdapter来操作字符串值:

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

}
输出

ABC
DEF
GHI
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<szm>
    <collection01>def</collection01>
    <collection01>ghi</collection01>
    <field01>abc</field01>
</szm>
ABC
DEF
GHI
def
ghi
abc
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<szm>
    <collection01>def</collection01>
    <collection01>ghi</collection01>
    <field01>abc</field01>
</szm>
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);
    }

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