JAXB:从Java代码更改XML元素的名称? [英] JAXB: Change name of XML Element from Java code?

查看:237
本文介绍了JAXB:从Java代码更改XML元素的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要设置XML元素的名称,我使用这样的注释:

To set the name of an XML element I use annotations like this:

@XmlElement(name = "customer_id")
public String getId(){}

我必须与两个不同的Web服务进行通信。一个表示名为 customer_id 的id元素,但另一个表示id元素的名称为 id 。我通过创建具有相同属性的第二个 Customer 类来解决问题。唯一不同的是它使用以下注释

I have to communicate with two different webservices. One expacts an id element named customer_id but the other expacts the id element to be named id. I solved the problem by creating a second Customer class with the same attributes. The only different is that it uses the following annotation

@XmlElement(name = "id")
public String getId(){}

它有一个复制构造函数,复制中的所有属性Customer1 Customer2 。当我与第一个webservice通信时,我发送一个 Customer1 对象,另一个webservice获取一个 Customer2 对象。

and it has a copy constructor which copies all attributes from Customer1 to Customer2. When I am communicating with the first webservice I send a Customer1 object and the other webservice gets a Customer2 object.

是否有可能只使用一个 Customer 对象,但重命名 id 属于webservice期望的任何内容?

Is there any possibility to use only one Customer object, but rename the id attribute to whatever the webservice expects?

推荐答案

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

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

您可以使用MOXy JAXB中的外部映射文档扩展名将第二个映射应用于对象模型。此映射文档可用于修改通过注释提供的元数据,或完全替换它。

You can use the external mapping document extension in MOXy JAXB to apply a second mapping to your object model. This mapping document can be be used to modify metadata provided via annotations, or completely replace it.

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

详细示例

在下面的示例中,单个对象模型映射到Google和Yahoo天气API的结果:

In the example below a single object model is mapped to the results of the Google and Yahoo weather APIs:

  • http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html

这篇关于JAXB:从Java代码更改XML元素的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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