• <small id='60owZ'></small><noframes id='60owZ'>

    <i id='60owZ'><tr id='60owZ'><dt id='60owZ'><q id='60owZ'><span id='60owZ'><b id='60owZ'><form id='60owZ'><ins id='60owZ'></ins><ul id='60owZ'></ul><sub id='60owZ'></sub></form><legend id='60owZ'></legend><bdo id='60owZ'><pre id='60owZ'><center id='60owZ'></center></pre></bdo></b><th id='60owZ'></th></span></q></dt></tr></i><div id='60owZ'><tfoot id='60owZ'></tfoot><dl id='60owZ'><fieldset id='60owZ'></fieldset></dl></div>
  • <legend id='60owZ'><style id='60owZ'><dir id='60owZ'><q id='60owZ'></q></dir></style></legend>
        • <bdo id='60owZ'></bdo><ul id='60owZ'></ul>
        <tfoot id='60owZ'></tfoot>

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

        时间:2023-09-28
          <i id='jxtx3'><tr id='jxtx3'><dt id='jxtx3'><q id='jxtx3'><span id='jxtx3'><b id='jxtx3'><form id='jxtx3'><ins id='jxtx3'></ins><ul id='jxtx3'></ul><sub id='jxtx3'></sub></form><legend id='jxtx3'></legend><bdo id='jxtx3'><pre id='jxtx3'><center id='jxtx3'></center></pre></bdo></b><th id='jxtx3'></th></span></q></dt></tr></i><div id='jxtx3'><tfoot id='jxtx3'></tfoot><dl id='jxtx3'><fieldset id='jxtx3'></fieldset></dl></div>
            <tbody id='jxtx3'></tbody>

          <tfoot id='jxtx3'></tfoot>
              <bdo id='jxtx3'></bdo><ul id='jxtx3'></ul>

              <small id='jxtx3'></small><noframes id='jxtx3'>

                <legend id='jxtx3'><style id='jxtx3'><dir id='jxtx3'><q id='jxtx3'></q></dir></style></legend>

                • 本文介绍了在 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 注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Spring,使用 @Configuration 和 @Bean 注解 下一篇:Eclipse 中覆盖注释的错误

                  相关文章

                  最新文章

                  <legend id='pavAn'><style id='pavAn'><dir id='pavAn'><q id='pavAn'></q></dir></style></legend>

                    <i id='pavAn'><tr id='pavAn'><dt id='pavAn'><q id='pavAn'><span id='pavAn'><b id='pavAn'><form id='pavAn'><ins id='pavAn'></ins><ul id='pavAn'></ul><sub id='pavAn'></sub></form><legend id='pavAn'></legend><bdo id='pavAn'><pre id='pavAn'><center id='pavAn'></center></pre></bdo></b><th id='pavAn'></th></span></q></dt></tr></i><div id='pavAn'><tfoot id='pavAn'></tfoot><dl id='pavAn'><fieldset id='pavAn'></fieldset></dl></div>

                  1. <tfoot id='pavAn'></tfoot>
                  2. <small id='pavAn'></small><noframes id='pavAn'>

                      <bdo id='pavAn'></bdo><ul id='pavAn'></ul>