gpt4 book ai didi

java - 从 Xmappr 更改为 BeanIO

转载 作者:行者123 更新时间:2023-12-02 12:16:42 24 4
gpt4 key购买 nike

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

exampleclass.java:

@Attribute("Code")
private String code;

它正在映射Brand元素的属性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 类?我需要将 xml 更改为:

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

然后在代码上使用@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.

关于java - 从 Xmappr 更改为 BeanIO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46116434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com