Changing from Xmappr to BeanIO

174 views Asked by At

When I want to specify the attribute for XML element in java class with Xmappr annotations, I use @Attribute annotation, e.g:

exampleclass.java:

@Attribute("Code")
private String code;

And it is mapping the attribute Code for Brand element

Brand.xml:

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

My task is to change the Xmappr annotations to BeanIO.

The single element(without attribute) I can map with @Field annotation, e.g:

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

So the question is, how do I get the attribute Code to java class with BeanIO? Do I need to change xml to:

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

and than use a @Field annotation on Code, or there is other way to do it?

1

There are 1 answers

0
nicoschl On BEST ANSWER

You can use

@Field(xmlType=XmlType.ATTRIBUTE)

Your code then becomes:

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

EDIT - expanding the answer

From the documentation:

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.

And in

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.