且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jaxb java类到多个xml映射

更新时间:2023-11-05 13:26:34

我认为您***的选择是使用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.