在UnMarshalling期间将Jaxb属性值转换为大写

时间:2016-10-18 14:30:57

标签: java jaxb unmarshalling xml-attribute xmladapter

我想在UnMarshalling期间将MyJaxbModel类中的 uid 属性值转换为大写。我确实编写了 UpperCaseAdapter 来为我工作。但是,使用这种方法,应用程序性能会恶化到不可接受的水平(因为有数千个XML文件被解组为MyJaxbModel)。我不能在getter / setter中使用String.toUppperCase(),因为这些JAXB模型是从XSD自动生成的,我不想调整它们。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "myJaxbModel")
public class MyJaxbModel
{
    protected String name;

    @XmlJavaTypeAdapter(UpperCaseAdapter.class)
    protected String uid;

    // getters and setters

}

public class UpperCaseAdapter extends XmlAdapter<String, String>
{
    @Override
    public String unmarshal( String value ) throws Exception
    {
        return value.toUpperCase();
    }

    @Override
    public String marshal( String value ) throws Exception
    {
        return value;
    }
}

<!--My XSD makes use of below xjc:javaType definition to auto-configure this-->
<xsd:simpleType name="uidType">
    <xsd:annotation>
        <xsd:appinfo>
            <xjc:javaType name="java.lang.String"
                adapter="jaxb.UpperCaseAdapter" />
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>

预期输入<myJaxbModel name="abc" uid="xyz" />

预期输出myJaxbModel.toString() -> MyJaxbModel[name=abc, uid=XYZ]

是否有更好的方法来达到预期的效果?

1 个答案:

答案 0 :(得分:0)

为什么不简单地在getUid()或设置时将其解析为大写?

if (uid != null){
   return uid.toUpperCase();
}
...

 ...
    if (uid != null){
       this.uir =  uid.toUpperCase();
    }

我认为这是更简单,最干净的方法......

相关问题