Java 编码器。使用标签值作为属性值

标签 java xml marshalling

我有课

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/

相关文章:

java - 无法在Java中读取阿拉伯文本文件

java - LinearLayout 的 onTouch() 函数不起作用

java - 使用 apache commons 解码Base64

java - 从 protected 网站导入 Java 中的 URL 图像

Android:文本大小更改时布局中断

java - 通过 xslt 的文本文件的 mime 类型

mysql - Dotnet Core Microsoft.EntityFrameworkCore.Tools.Dotnet 未安装

json - 如何在 Go json marshal 中显示空对象而不是空结构或 nil

json - 您如何编码 sql.NullString 以便输出被展平以仅给出 go 中的值?

c# - 从 c# LPCTSTR 编码不再工作