I would like to parse this XML to java pojo using Jackson dataformat X" /> I would like to parse this XML to java pojo using Jackson dataformat X" /> I would like to parse this XML to java pojo using Jackson dataformat X"/>

Parsing XML element without children but with attributes using jackson

45 Views Asked by At

I have an xml element

<root>
    <name1 p="xyz" b ="678"/>
    <name2 p="abc"/>
</root>

I would like to parse this XML to java pojo using Jackson dataformat XML library. But i need to do that without creating a class for name1 element or name2 element because i just want the attribute values and name1/name2 tags will not have any children.

I tried using isAttribute = true over jacksonXmlproperty annotation. But I will not be able to give the fieldname which i want in java class, because we have to give the same name as attribute name. And that would conflict with name1 attribute p and name2 attribute p. This approach will result in a java class like below

class Root {
    
    @JacksonXmlproperty(localName="name1" isAttribute=true)
    String p;

    @JacksonXmlproperty(localName="name2" isAttribute=true)
    String p;         // another attribute with same name p in the name2 element.

}

I am not sure if we need to use any custom deserializer to acheive this.

Can anyone help on this? Thanks in advance !

0

There are 0 best solutions below