从 Xmappr 更改为 BeanIO

Changing from Xmappr to BeanIO

当我想用Xmappr注解为java class中的XML元素指定属性时,我使用@Attribute注解,例如:

exampleclass.java:

@Attribute("Code")
private String code;

它正在为 品牌 元素

映射属性 代码

Brand.xml:

<Brand Code="123">
    <Description>Name</Description>
    <BrandName>true</BrandName>
</Brand>

我的任务是将 Xmappr 注释更改为 BeanIO。

我可以用@Field注解映射的单个元素(没有属性),例如:

@Field(xmlName="Description")
Private String description;

所以问题是,如何使用 BeanIO 将属性 Code 设置为 java class? 我需要将 xml 更改为:

<Brand>
    <BrandCode>123</BrandCode>
    <Description>Name</Description>
    <BrandName>true</BrandName>
</Brand>

然后在 Code 上使用 @Field 注释, 或者还有其他方法吗?

您可以使用

@Field(xmlType=XmlType.ATTRIBUTE)

您的代码将变为:

@Field(xmlName="Code", xmlType=XmlType.ATTRIBUTE)
private String code;

编辑 - 扩展答案

来自文档:

5.7 Fields

A field is mapped to XML using the field's xmlType attribute, which defaults to element. The field XML type can be set to element, attribute, text, or none.

并且在

6.2 Annotations

When using annotations, it is strongly recommended to explicitly set the position (using at) for all fields and segments. BeanIO does not guarrantee the order in which annotated components are added to a layout.

Annotation settings are generally named according to their mapping file counterparts and follow the same convention as well.