Search code examples
javaxmljackson-dataformat-xml

How to express XML with mixed content using Jackson XmlMapper


I need write a Java program that will generate a XML with mixed content. It should more or less like this:

   <foo>
       <bar><a></a>Some text<b></b></bar>
    </foo>

I would like to use Jackson XML Mapper to serialize Java objects to XML but I don't know how to build an object equivalent to above XML.


Solution

  • Eventually I was able to generated expected XML using following Java record:

    record foo(
            @JacksonXmlProperty
            Bar bar
    ){};
    
    @JsonPropertyOrder({"a", "content", "b"})
    record Bar(
            @JsonInclude
            @JacksonXmlProperty
            String a,
            @JacksonXmlText
            String content,
            @JsonInclude
            @JacksonXmlProperty
            String b
    ) {
    };