结合使用JAXB和Character [英] Using JAXB with Character

查看:58
本文介绍了结合使用JAXB和Character的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个@XMLElement字符类型,但是当它被编组时,它似乎被放入二进制字符串中,例如...

I have an @XMLElement that is of type Character but when it get marshalled it appears to get put into a binary string so for example...

'n' becomes 110
'e' becomes 101

将它们转换为字符串的简短方式是否可以输出文本char而不是表示形式?

Short of converting them to Strings is there a way I can output the text char instead of the representation?

推荐答案

您可以编写XmlAdapter. XmlAdapter允许您将一种对象类型转换为另一种对象,以进行编组/解组.

You could write an XmlAdapter. An XmlAdapter allows you to convert one type of object to another for the purposes of marshalling/unmarshalling.

XmlAdapter(CharacterAdapter)

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class CharacterAdapter extends XmlAdapter<String, Character> {

    @Override
    public Character unmarshal(String v) throws Exception {
        return v.charAt(0);
    }

    @Override
    public String marshal(Character v) throws Exception {
        return new String(new char[] {v});
    }

}

Java模型

使用@XmlJavaTypeAdapter注释指定XmlAdapter:

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Foo {

    private Character bar;

    @XmlJavaTypeAdapter(CharacterAdapter.class)
    public Character getBar() {
        return bar;
    }

    public void setBar(Character bar) {
        this.bar = bar;
    }

}

更多信息

  • http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
  • http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html

这篇关于结合使用JAXB和Character的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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