如何指定JAXB用于编组/解组数据的适配器? [英] How do I specify the adapter(s) which JAXB uses for marshaling/unmarshaling data?

查看:90
本文介绍了如何指定JAXB用于编组/解组数据的适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法指定JAXB用于在我的XML模式中封送/解组对象的适配器?

Is there a way to specify the Adapter which JAXB uses for marshaling/unmarshaling objects in my XML schema?

例如,如果我想解析以下内容整数:

For example, if I want to parse the following as an integer:

<SpecialValue>0x1234</SpecialValue>

我可以在我的架构中使用以下内容:

I can use the following in my schema:

<xs:simpleType name="HexInt">
    <xs:annotation> 
        <xs:appinfo>
            <jaxb:javaType name="int" parseMethod="Integer.decode" />
        </xs:appinfo>
    </xs:annotation>  
    <xs:restriction base="xs:string">
        <xs:pattern value="0x[0-9a-fA-F]+" />
    </xs:restriction>
</xs:simpleType>
<xs:element name="SpecialValue" type="HexInt" />

当我通过XJC工具运行模式时,应使用Integer解码字符串0x1234。 decode()为整数,值为0x1234,或十六进制为4660。生成的Adapter类正确反映了这一点:

When I run the schema through the XJC tool, the String "0x1234" should be decoded using Integer.decode() as the Integer with value 0x1234, or 4660 in decimal. The Adapter class that is generated reflects this properly:

public Integer unmarshal(String value) {
    return (Integer.decode(value));
}

但是,当我想将值封送回XML元素时(是一个String文字),Adapter类执行以下操作:

However, when I want to marshal the value back to an XML element (which is a String literal), the Adapter class does the following:

public String marshal(Integer value) {
    if (value == null) {
        return null;
    }
    return value.toString();
}

在这种情况下,整数0x1234的字符串值(十进制4660)是4660,它不符合我的架构(因为它没有0x前缀)。

In this case, the String value of the integer 0x1234 (4660 in decimal) is "4660", which does not adhere to my schema (because it has no "0x" prefix).

如何告诉适配器我想要整数0x1234被编组为0x1234字符串文字?我希望能够在模式中执行此操作,以便我可以生成新的Java类而无需修改输出。这可能吗?

How can I tell the Adapter that I want the integer 0x1234 to be marshalled as the "0x1234" String literal? I would be like to be able to do this within the schema so that I can just generate new Java classes without having to modify the output. Is this possible?

编辑:根据接受的答案,以下是我为此付出的努力:

Based on the accepted answer, here is what I did to get this working:

我在com.example.Parse类中编写了一个名为toHexString()的方法:

I wrote a method in a class com.example.Parse called toHexString():

public static String toHexString(int value)
{
    return ("0x" + Integer.toHexString(value));
}

然后我将模式指向该方法进行打印:

Then I pointed my schema to that method for printing:

<xs:simpleType name="HexInt">
    <xs:annotation> 
        <xs:appinfo>
            <jaxb:javaType name="int" parseMethod="Integer.decode" printMethod="com.example.Parse.toHexString" />
        </xs:appinfo>
    </xs:annotation>  
    <xs:restriction base="xs:string">
        <xs:pattern value="0x[0-9a-fA-F]+" />
    </xs:restriction>
</xs:simpleType>


推荐答案

试试这个

<jaxb:javaType name="int" parseMethod="Integer.decode" 
                          printMethod="Integer.toHexString"/>

我还没有测试过,但我记得使用的东西非常相似。

I haven't tested it though but I remember using something very similar.

这篇关于如何指定JAXB用于编组/解组数据的适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆