JAXB(联合国)xsd类型的编组:xsd:base64Binary和xsd:hexBinary

编程入门 行业动态 更新时间:2024-10-20 00:47:17
本文介绍了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.apachemons.codec.binary.Base64.decodeBase64(s); } public String marshal(byte[] bytes) { if (bytes == null) return null; return org.apachemons.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

本文发布于:2023-11-25 23:56:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631844.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:联合国   类型   JAXB   xsd   hexBinary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!