coolsv
coolsv

Reputation: 268

Map XML attribute to specified tag(field) in Jackson Xml Mapper

I have a class:

public class SomeClass {

    public String a = "tag"

    @JacksonXmlProperty(isAttribute = true)
    public String b = "attribute"

}

I need to set variable b as a property of variable a:

<SomeClass>
     <a b="attribute">tag</a>
</SomeClass>

I tried @JacksonXmlProperty(isAttribute = true), but it maps my attribute only to the root tag:

  <SomeClass b="attribute">
         <a>tag</a>
  </SomeClass>

It there a way to handle this problem with annotations?

Upvotes: 0

Views: 1292

Answers (1)

Ori Marko
Ori Marko

Reputation: 58862

Create a new class A

public class A {
  @JacksonXmlProperty(isAttribute = true)
  public String b = "attribute"
}

And use it in your class:

public class SomeClass {
  @JacksonXmlProperty
  public A a = "tag"

}

Upvotes: 1

Related Questions