45

I can't seem to find a way to make a Pojo Using the jackson-xml annotations that would generate xml like the following:

<Root>
    <Element1 ns="xxx">
        <Element2 ns="yyy">A String</Element2>
    </Element1>
</Root>

The closest I can seem to come is the following:

Root POJO:

public class Root {
    @JacksonXmlProperty(localName = "Element1")
    private Element1 element1;

    public String getElement1() {
        return element1;
    }

    public void setElement1(String element1) {
        this.element1 = element1;
    }
}

Element1 POJO:

public class Element1 {
    @JacksonXmlProperty(isAttribute = true)
    private String ns = "xxx";
    @JacksonXmlProperty(localName = "Element2")
    private Element2 element2;

    public String getElement2() {
        return element2;
    }

    public void setElement2(String element2) {
        this.element2 = element2;
    }
}

Element2 POJO:

public class Element2 {
    @JacksonXmlProperty(isAttribute = true)
    private String ns = "yyy";
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

But this returns back the following:

<Root>
    <Element1 ns="xxx">
        <Element2 ns="yyy"><value>A String</value></Element2>
    </Element1>
</Root>

The element tags around "A String" I do not want to display.

1
  • I'm curious as to why you're using Jackson-specific annotations and not JAXB annotations. Jackson is primarily a framework for producing JSON, not XML, and as such, misses some of the features of JAXB. Commented Nov 7, 2013 at 21:39

3 Answers 3

78

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Turns out I was importing from the wrong dependency, and getting and older version of the jackson-dataformat-xml where that annotation wasn't included :\
Understandable. As fantastic as Jackson is, their documentation is horrendous. So it is very hard to find which versions are where and which annotations are available with those versions.
I've definitely dug through their documentation before, and I know what you mean, but this one is definitely on me - Their dependency is blatantly mentioned on their github page: github.com/FasterXML/jackson-dataformat-xml. I was looking for the gradle dependency, and googled & pasted too fast.
@llya : Do we not need @JacksonXmlRootElement on Root class ?
Awesome when someone provides EXACTLY what you were looking for. +1
6

To complement flyingAssistant's answer, 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 repository. 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
}

1 Comment

Thanks man, you saved my time, eventhough thousands ChatGPT like helps comes, human helps is inevitable
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.