如何在运行时使用JAXB注释 [英] How to use JAXB annotations at runtime
本文介绍了如何在运行时使用JAXB注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下bean类
@XmlRootElement(name = "book")
//Optional
@XmlType(propOrder = {"name" })
public class Book {
private String name;
private int num;
@XmlTransient
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
// name for your XML-Output:
@XmlElement(name = "bookName")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以及相应的编组代码
private static void marshalXML(Book bookstore) {
Writer w = null;
try {
// create JAXB context and instantiate marshaller
JAXBContext context = getContext();
if (context != null) {
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(bookstore, System.out);
w = new FileWriter(BOOKSTORE_XML);
m.marshal(bookstore, w);
}
} catch (Exception e) {
System.out.println("error in marshalling");
} finally {
try {
w.close();
} catch (Exception e) {
}
}
}
我想让属性在运行时可配置,我想在运行时指定@xmltransientnum而不是编译时间。我可以这样做吗?
I want to make the attributes configurable at runtime ,i want to specify @xmltransient on "num" at runtime not compile time.how can i do it?
推荐答案
注意:我是 EclipseLink JAXB(MOXy) 领导者和 JAXB 2(JSR-222)专家组成员。
MOXy JAXB实现提供了在运行时通过其 MetadataSource
扩展来操作映射元数据的功能。有关详细示例,请参阅:
The MOXy JAXB implementation offers the ability to manipulate the mapping metadata at runtime via its MetadataSource
extension. For a detailed example see:
- http://blog.bdoughan.com/2011/06/moxy-extensible-models-refresh-example.html
这篇关于如何在运行时使用JAXB注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文