'How to map nested xml elements to string in java using @xmlElement

I trying to map xml element object to string by using @XmlElement

sample xml:

<element1>
  <element2>
     <element3> text </element3>
  </element2>
<element1>

Java code:

@Getter
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "element1", namespace = "")
@XmlAccessorType(XmlAccessType.FIELD)
public class ElementData
{
  @XmlElement(name = "element2")
  private String element2;
}

Expected output: Map element2 to string in the java class ElementData



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source