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

查看:48
本文介绍了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){

        }

我收到以下异常.一个用于无参数构造函数(我知道我可以通过为 Adapter 添加注释来解决这个问题,但我想知道或查看一个无需注释即可处理它的示例代码片段)

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 次 IllegalAnnotationExceptionsjava.sql.Timestamp 没有无参数的默认构造函数.此问题与以下位置有关:在 java.sql.Timestamp在公共 java.sql.Timestamp com.test.Abc.getSomeTimestamp()在 com.riteaid.entities.customer.Customercom.test.Def 没有无参数的默认构造函数.此问题与以下位置有关:在 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 没有无参数的默认构造函数.此问题与以下位置有关:在 java.sql.Date...com.test.Ghi 是接口,JAXB 不能处理接口.此问题与以下位置有关:...com.test.Ghi 没有无参数的默认构造函数.此问题与以下位置有关:..

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:

提供元数据的标准方式是通过注释.3rd 方类等无法标注的情况,那么你可能会对 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.

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

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

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

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