Jackson XML Annotations: String element with attribute
You should use JacksonXmlText annotation for value
field.
public class Element2
{
@JacksonXmlProperty(isAttribute = true)
private String ns = "yyy";
@JacksonXmlText
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
then XML will looks like
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy">A String</Element2>
</Element1>
</Root>
Unfortunately I'm unable to comment but I did want to make a note about flyingAssistant's answer in case anyone else ran into the same issue I was having. You can't add @JacksonXmlText to a constructor property. This feature may be added in build 2.13 based off of this issue reported in the GitHub repo. So for now you'll have to do this
data class Element2(@field:JacksonXmlProperty(isAttribute = true) val ns: String = "yyy") {
@field:JacksonXmlText
val value: String? = null
}
For Kotlin, you need to use @field
annotation use-site targets:
data class Element2(
@field:JacksonXmlProperty(isAttribute = true)
val ns: String = "yyy",
@field:JacksonXmlText
val value: String? = null
)
If you don't like defining initial values for ns
and value
properties by yourself then use Kotlin no-args plugin, which generates a default constructor instead.