JAXB(联合国)xsd类型的编组:xsd:base64Binary和xsd:hexBinary [英] JAXB (un)marshalling of xsd types: xsd:base64Binary and xsd:hexBinary

查看:129
本文介绍了JAXB(联合国)xsd类型的编组:xsd:base64Binary和xsd:hexBinary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JAXB将 xsd:base64Binary xsd:hexBinary 类型映射到 byte []

JAXB maps both xsd:base64Binary and xsd:hexBinary types to byte[].

鉴于我有一个架构/一个DOM元素代表这些类型中的每一个,例如:

Given that I have a schema/a DOM Element representing each of these types such as:

< foo> ABCD< / foo> 用于xsd:hexBinary和

< foo> YTM0NZomIzI2OTsmIzM0NTueYQ ==< / foo> 对于xsd:base64Binary,

<foo>ABCD</foo> for xsd:hexBinary and
<foo>YTM0NZomIzI2OTsmIzM0NTueYQ==</foo> for xsd:base64Binary ,

目前还不清楚JAXB 2.1如何处理它。

it's not clear how JAXB 2.1 handles it.

JAXB.unmarshal(新的DOMSource(节点),byte [] .class)不喜欢有效载荷。

以下内容均不包括:

JAXB.unmarshal(new DOMSource(node), byte[].class) does not like the payload.
Neither does the following:

JAXBContext ctx = JAXBContext.newInstance(byte [] .class);
ctx.createUnmarshaller()。unmarshal(node);

处理这些类型的正确方法是什么?
提前致谢。

What's the correct way of handling these types? Thanks in advance.

推荐答案

byte []与hexBinary或base64Binary表示之间的转换是通过通讯员完成的XmlAdapter。

The conversion between byte[] and the hexBinary or base64Binary representation is done via a correspondent XmlAdapter.

默认情况下,JAXB使用包含的 HexBinaryAdapter ,用于将byte []转换为String。
我不知道是否还有一个XmlAdapter从/转换为base64但是没问题:

by default JAXB uses the included HexBinaryAdapter for converting byte[] to a String. I don't know if there is also an XmlAdapter which converts from/to base64 but that is no problem:

你可以使用一个自己轻松实现它拥有XmlAdpater:

You can easily implement it yourself using an own XmlAdpater:

public final class Base64Adapter extends XmlAdapter<String, byte[]> {
    public byte[] unmarshal(String s) {
        if (s == null)
            return null;
        return org.apache.commons.codec.binary.Base64.decodeBase64(s);
    }

    public String marshal(byte[] bytes) {
        if (bytes == null)
            return null;
        return org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
    }
}

你可以在字段/ getter_setter级别指定应该是什么由哪个适配器处理:

You can specify on a field/getter_setter level what should be processed by which adapter:

private class DataTestClass {

    @XmlJavaTypeAdapter(Base64Adapter.class)
    public byte[] base64Data = new byte[] { 0, 1, 2, 3, 4 };

    @XmlJavaTypeAdapter(HexBinaryAdapter.class)
    public byte[] hexbinData = new byte[] { 0, 1, 2, 3, 4 };

}

这篇关于JAXB(联合国)xsd类型的编组:xsd:base64Binary和xsd:hexBinary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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