我有课
class Header {
@FCBTag(type="type1") --My custom annotation
int a = "valueA";
@FCBTag(type="type2")
String b = 1;
@FCBTag(type="type3")
Boolean c = true;
}
我想像这样将这个类编码为 XML
<Header>
<a type="type1" value="valueA" />
<b type="type2" value="1" />
<c type="type2" value="true" />
</Header>
这可能吗?是否存在一些适配器?我该如何做到这一点?
最佳答案
我在 EclipseLink JAXB (MOXy's) 中实现了对此的支持@XmlPath
扩展名。以下是您在类上指定映射的方法。
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlAccessorType(XmlAccessType.FIELD)
class Header {
@XmlPath("a[type='type1']/@value")
int a = "valueA";
@XmlPath("b[type='type2']/@value")
String b = 1;
@XmlPath("c[type='type3']/@value")
Boolean c = true;
}
了解更多信息
我的博客上有此功能的完整示例:
关于Java 编码器。使用标签值作为属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23290694/