在 JAXB 中,如何使用 @XmlJavaTypeAdapters 注解?

In JAXB, how to use @XmlJavaTypeAdapters annotation?(在 JAXB 中,如何使用 @XmlJavaTypeAdapters 注解?)
本文介绍了在 JAXB 中,如何使用 @XmlJavaTypeAdapters 注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

从xml解组到JAXB的Java类时,我想将javax.xml.datatype.XMLGregorianCalendar"更改为java.util.Date".

I want to change "javax.xml.datatype.XMLGregorianCalendar" to "java.util.Date" when unmarshalling from xml to Java class of JAXB.

但我没有在 Java 类中添加任何 @XmlJavaTypeAdapter 的注解.

But I don't put any annotations of @XmlJavaTypeAdapter in Java classes.

所以,我打算尝试使用@XmlJavaTypeAdapters的注解,但是不知道怎么用……

So, I'm going to try to use an annotation of @XmlJavaTypeAdapters, but I don't know how to use it...

请给我展示使用它的例子.

Please show me examples for using it.

推荐答案

顺便说一句,您实际上不需要将 XMLGregorianCalendar 适配到 Date,因为 JAXB 本身就支持 java.util.Date —— 像这样:

As an interesting aside, you don't actually need to adapt XMLGregorianCalendar to Date, since JAXB supports java.util.Date natively -- like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Example {
    @XmlSchemaType(name = "date")
    public Date publishingDate;
}

如果你需要它,@XmlJavaTypeAdapter 可以像这样工作,假设你的自定义类:

If you need it, @XmlJavaTypeAdapter can work like this, assuming your custom class:

public class SillyDate {
    public SillyDate(int year, int month, int day) {
        super();
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public String toString() {
      return "SillyDate [year=" + year + ", month=" + month + ", day=" + day + "]";
    }

    public int year;
    public int month;
    public int day;
}

您需要一个 JAXB 可以理解的类,然后在该类和自定义类之间编写一个适配器,如下所示:

You need a class which JAXB can understand, and then write an adapter between that class and the custom class, like this:

public class SillyDateAdapter extends XmlAdapter<XMLGregorianCalendar, SillyDate> {
    public SillyDate unmarshal(XMLGregorianCalendar val) throws Exception {
      return new SillyDate(val.getYear(), val.getMonth(), val.getDay());
    }

    public XMLGregorianCalendar marshal(SillyDate val) throws Exception {
      return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(val.year, val.month, val.day, 0);
    }
}

现在您可以在自己的类中使用它,如下所示:

Now you can use that in your own classes, like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Example2 {
    @XmlSchemaType(name = "date")
    @XmlJavaTypeAdapter(type=XMLGregorianCalendar.class,value =SillyDateAdapter.class)
    public SillyDate publishingDate;
}

网上有很多使用@XmlJavaTypeAdapter 的好例子,比如这个 和 这个一个,还有其他几个.适应愉快!

There are plenty of good examples of using the @XmlJavaTypeAdapter available on the net, like this one and this one, and several others. Happy adapting!

这篇关于在 JAXB 中,如何使用 @XmlJavaTypeAdapters 注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)