如何在运行时使用 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) {
}
}
}
我想在运行时配置属性,我想在运行时在num"上指定@xmltransient,而不是编译时.我该怎么做?
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:
这篇关于如何在运行时使用 JAXB 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文