自定义 JAXB xml 输出
给定以下类:
public class Customer {
public String name;
public String lastName;
}
我想使用 JAXB 为 name
为 John、lastName
为 Doe 的客户生成以下 xml 输出:
<cst>John Doe</cst>
我如何使用 JAXB 执行此操作?
编辑
类Customer
在多个地方使用,如下所示:
public class Sale {
private String productId;
private Date date;
private Customer customer;
}
public class Transaction {
private List<Sale> sales;
}
...等等...问题是,如何才能我告诉 JAXB:“每当您见到客户时,请使用自定义格式”?
我的问题是有很多类包含客户,我想以编程方式控制输出(有时 name + lastname
,有时
、
),而无需在包含 Customer
的每个类中添加注释。此要求将排除使用 JAXBElement
。
Given the following class:
public class Customer {
public String name;
public String lastName;
}
I want to generate the following xml output using JAXB for a customer whose name
is John and lastName
is Doe:
<cst>John Doe</cst>
How can i do this with JAXB?
EDIT
The class Customer
is used in several places, as shown here:
public class Sale {
private String productId;
private Date date;
private Customer customer;
}
public class Transaction {
private List<Sale> sales;
}
... and so on... The deal is, how can I tell JAXB: "whenever you see a customer, please use custom formatting"?
My problem is that there are many classes that contain a customer, and I want to programatically control the output (sometimes name + lastname
, sometimes <name>name</name>
, <lastname>lastname</lastname>
) without adding annotations at every class that contains Customer
. This requirement would rule out using JAXBElement<Customer>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以安装一个处理翻译的
XmlAdapter
:输出
You could install an
XmlAdapter
that handles the translation:outputs