JAXB解组没有注释的自定义实体 [英] JAXB unmarshalling Custom entities without annotation

查看:133
本文介绍了JAXB解组没有注释的自定义实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个xml文件,我们需要解组(转换为Java对象)。现在Java对象属于第三方,我无法对其进行解组以进行解组。任何关于如何在没有注释的情况下解组的想法。请在下面找到我的代码段

We have an xml file which we need to unmarshall(convert into a Java Object). Now the Java object is of third party and I cannot annotate it for unmarshalling. Any idea as to how I can Unmarshal without annotation. Please find my code snippet below

JAXBContext context;
        try {
            context = JAXBContext.newInstance(Abc.class);
            Unmarshaller unMarshaller = context.createUnmarshaller();
            Abc abc= (Abc) unMarshaller.unmarshal(new FileInputStream("C:\\Documents and Settings\\sandeep.nair\\Desktop\\abc.xml"));
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (Exception e){

        }

I我得到以下例外。一个没有参数构造函数(我知道我可以通过添加适配器的注释来解决这个问题,但我想知道或看到一个样本片段来处理它没有注释)

I am getting the following exception. One for No argument Constructor(I know I can solve this by adding annotation for Adapter but I want to know or see a sample snippet for handling it without annotation)

同样我我收到另一条消息,说JAXB无法处理接口。

Similarly I am getting another message as to Interfaces cannot be handle by JAXB.

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:5个IllegalAnnotationExceptions计数
java.sql.Timestamp没有no-arg默认构造函数。
此问题与以下位置有关:
at java.sql.Timestamp
at public java.sql.Timestamp com.test.Abc.getSomeTimestamp()
at com。 riteaid.entities.customer.Customer
com.test.Def没有no-arg默认构造函数。
此问题与以下位置有关:
at com.test.Def ...

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 5 counts of IllegalAnnotationExceptions java.sql.Timestamp does not have a no-arg default constructor. this problem is related to the following location: at java.sql.Timestamp at public java.sql.Timestamp com.test.Abc.getSomeTimestamp() at com.riteaid.entities.customer.Customer com.test.Def does not have a no-arg default constructor. this problem is related to the following location: at com.test.Def...

java.sql.Date没有no-arg默认构造函数。
此问题与以下位置有关:
at java.sql.Date
...
com.test.Ghi是一个接口,JAXB无法处理接口。
此问题与以下位置有关:
...
com.test.Ghi没有no-arg默认构造函数。
此问题与以下位置有关:
..

java.sql.Date does not have a no-arg default constructor. this problem is related to the following location: at java.sql.Date ... com.test.Ghi is an interface, and JAXB can't handle interfaces. this problem is related to the following location: ... com.test.Ghi does not have a no-arg default constructor. this problem is related to the following location: ..

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222)的成员专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

JAXB是异常配置,因此您只需要在要覆盖默认映射行为的地方添加注释:

JAXB is configuration by exception , so you only need to add annotations where you want to override the default mapping behaviour:

  • http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html

提供元数据的标准方法是通过注释。在您无法注释诸如第三方课程的情况下,您可能对MOXy的外部地图文档扩展感兴趣。

The standard way of supplying metadata is through annotations. In situations where you can't annotate such as 3rd party classes, then you may be interested in MOXy's external mapping document extension.

  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
  • http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html

您可以使用 XmlAdapter 来处理 javax.sql.Date javax.sql.Timestamp 类型:

You can use an XmlAdapter to handle the javax.sql.Date and javax.sql.Timestamp types:

  • jaxb unmarshal timestamp

这篇关于JAXB解组没有注释的自定义实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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