1

I want to parse xml to java object two identical xml with the only root tag name different:

<root1>
    <a></a>
    <b></b>
    <c></c>
</root1>
<root2>
    <a></a>
    <b></b>
    <c></c>
</root2>

It is possible to do it using only one pojo annotated with something like

@JacksonXmlRootElement(localName = "root1|root2") ?

ozzem
  • 294
  • 1
  • 3
  • 16

1 Answers1

0

With the inspiration/idea from JacksonXmlRootElement with dynamic localName value, I used the interface based solution.

I declared the main (data) class Root, which I did not annotate with @JacksonXmlRootElement. Then declared two interfaces, Root1 and Root2 with both @JacksonXmlRootElement and made Root to implement them both.

The solution in Kotlin would be:

@JacksonXmlRootElement(localName = "root1")
interface Root1

@JacksonXmlRootElement(localName = "root2")
interface Root2

data class Root(
    @field:JacksonXmlProperty(localName = "a") val a: String,
    @field:JacksonXmlProperty(localName = "b") val b: String,
    @field:JacksonXmlProperty(localName = "c") val c: String
): Root1, Root2