jaxb java类到多个xml映射 [英] jaxb java class to multiple xml mappings

查看:117
本文介绍了jaxb java类到多个xml映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jaxb将一些java类转换为xml。 (我不需要根据模式生成类)我需要能够将类映射到不同的xml格式,所以我不想使用注释。从我所看到的,我最好的选择似乎是使用外部xml绑定。所以我想知道:

I have a couple of java classes that I want to convert to xml using jaxb. (I have no need to generate the classes based on the schema) I need to be able to map the class to different xml formats so I do not want to use annotations. From what I've seen my best option seems to be to use external xml bindings. So I wanted to know:

1)我正在使用eclipse。我是JAXB的新手,我想知道如何使用eclipse集成外部绑定?

1) I am using eclipse. I am new to JAXB and I would like to know how to integrate external bindings using eclipse?

2)除了外部xml绑定之外还有哪些其他选项?

2) What other options other than external xml bindings are available?

推荐答案

我认为您最好的选择是使用MOXy XML绑定:

I think you best option is to use MOXy XML bindings:

  • http://www.eclipse.org/eclipselink/documentation/2.6/moxy/runtime003.htm

这允许您在表单中定义XML< - > Java映射XML文件而不是注释:

This allows you to define XML<->Java mappings in form of XML files instead of annotations:

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="example">

    <java-types>
        <java-type name="Customer">
            <xml-root-element name="customer-info" />
            <java-attributes>
                <xml-attribute java-attribute="custId" name="customer-id" />
                <xml-element java-attribute="picture" name="picture-hex">
                    <xml-schema-type name="hexBinary" />
                    <xml-java-type-adapter
                        value="example.adapters.MyHexConverter" />
                </xml-element>
            </java-attributes>
        </java-type>
    </java-types>

</xml-bindings>

您可以通过 JAXBContextProperties.OXM_METADATA_SOURCE property:

You can use this file via JAXBContextProperties.OXM_METADATA_SOURCE property:

Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);

JAXBContext ctx = JAXBContext.newInstance(new Class[] { Customer.class }, properties);

因此,如果您想为同一个类创建多个不同的映射,只需编写几个XML绑定并创建 JAXBContext 以及相应的文件。我认为这是目前最好的选择,使用MOXy

So if you want several different mappings for the same class, just write several XML bindings and create your JAXBContext with corresponding files. I think this is the best options right now, with MOXy

使用纯JAXB RI,您可以编写自己的注释阅读器。我用Annox做过一次:

With pure JAXB RI you can write an own annotations reader. I did this once with Annox:

  • http://confluence.highsource.org/display/ANX/JAXB+User+Guide

另一个选项是JBoss JAXBIntroductions,也基于自定义注释阅读器:

Another option was JBoss JAXBIntroductions, also based on a custom annotations reader:

  • https://developer.jboss.org/wiki/JAXBIntroductions

但我不确定它是否已经存在。

But I'm not sure this is live anymore.

因为你需要多个映射,所以你必须写它们(除了一个之外) )手动。您可以生成一组映射作为注释,但必须手动编写其他映射。或者,比方说,我不知道会产生一个插件或工具,例如MOXy XML绑定。写一个不是一个大问题。

Since you want multiple mappings, you'll have to write them (all but one) manually. You can generate one set of mappings as annotations, but further mappings will have to be written manually. Or, let's say, I'm not aware of a plugin or tool which would generate, for instance, MOXy XML bindings. Wouldn't be a big problem to write one though.

你也可以采取一种完全不同的方法。您可以将单独的DTO包映射到这些格式,而不是使用不同的映射/格式映射一个中央模型。然后在您的DTO和中央模型之间进行转换。类似

You may also take a completely different approach. Instead of mapping one central model with different mappings/format, you can have map a separate package of DTOs onto these formats. And then convert between your DTOs and the central model. Something like

XML(1) <-> DTO(1)|<-\
XML(2) <-> DTO(2)|<--*->Model
XML(3) <-> DTO(3)|<-/

因此,每种交换格式都有干净的DTO(其中您可以生成模式)和单个中央业务模型(统治它们)。您必须在DTO和模型之间进行转换,这可以使用像Dozer这样的工具来处理。

Thus you'll have clean DTOs per exchange format (which you can generate out of schemas) and a single central business model (to rule them all). You'll have to convert between DTOs and the Model, this can be handled with a tool like Dozer.

如果这是一个更好的方法,取决于你的复杂程度格式是以及彼此之间的差异。

If this is a better approach or not depends on how complex your formats are and how different they are fron one another.

关于你的问题:

1)没什么特别的关于Eclipse,只需添加MOXy作为依赖项并按照文档进行操作。

2)我已经描述了上面几个选项。

1) There's nothing special about Eclipse, just add MOXy as dependency and follow the docs.
2) I've described a few options above.

这篇关于jaxb java类到多个xml映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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