0

我想在 UnMarshalling 期间将 MyJaxbModel 类中的uid属性值转换为大写。我确实编写了为我工作的UpperCaseAdapter。然而,使用这种方法,应用程序性能会下降到无法接受的程度(因为有数千个 XML 文件未编组到 MyJaxbModel)。我不能String.toUppperCase()在 getter /setter 中使用,因为这些 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]

有没有更好的方法来达到预期的结果?

4

1 回答 1

0

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

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

或者

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

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

于 2016-10-18T14:41:39.057 回答