jaxb,集合的单个项目设置器

发布于 2024-11-07 16:55:02 字数 814 浏览 4 评论 0原文

我想确保 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;

提前致谢, Agostino

全班同学,以防万一:

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;

}

I want to make sure that an xml element-content is unmarshalled in upper case on my object.

public class SZM {

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

but how to do the same thing for every item in a collection? I want that any value read from the xml is capitalized.

@XmlElement
ArrayList<String>collection01;

Thanks in advance,
Agostino

all the class, just in case:

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-11-14 16:55:02

您可以使用 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();
    }

}

SZM

您可以将 XmlAdapter 引用为:

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>

Demo >

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>

You can use an XmlAdapter to manipulate the String values:

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

You reference the XmlAdapter as:

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>

Demo

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

}

Output

ABC
DEF
GHI
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<szm>
    <collection01>def</collection01>
    <collection01>ghi</collection01>
    <field01>abc</field01>
</szm>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文