Dillon Ryan Redding
Dillon Ryan Redding

Reputation: 1243

Jackson XML List Elements Match Their Root Name

Using Jackson's XmlMapper, is there a way to have the names of elements of a list of objects match the localName given in @JacksonXmlRootElement?

For example, given the following classes

interface Foo {}

@JacksonXmlRootElement(localName = "bar")
class Bar implements Foo {}

@JacksonXmlRootElement(localName = "baz")
class Baz implements Foo {}

@JacksonXmlRootElement(localName = "container")
class FooContainer {
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Foo> foos;

    FooContainer(List<Foo> foos) {
        this.foos = foos;
    }

    public List<Foo> getFoos() {
        return foos;
    }
}

Using the XmlMapper

FooContainer container = new FooContainer(Arrays.asList(new Bar(), new Baz()));
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValueAsString(container);

We get the following XML

<container>
  <foos/>
  <foos/>
</container>

Is there a way to instead get this?

<container>
  <bar/>
  <baz/>
</container>

Upvotes: 4

Views: 1834

Answers (1)

Dillon Ryan Redding
Dillon Ryan Redding

Reputation: 1243

I haven't been able to find a way to do this in Jackson, but it's almost trivial in JAXB.

interface Foo {}

class Bar implements Foo {}

class Baz implements Foo {}

@XmlRootElement(name = "container")
@XmlAccessorType(XmlAccessType.FIELD)
class FooContainer {
    @XmlElements({
        @XmlElement(name = "bar", type = Bar.class),
        @XmlElement(name = "baz", type = Baz.class)
    })
    private List<Foo> foos;

    FooContainer() {}

    FooContainer(List<Foo> foos) {
        this.foos = foos;
    }

    public List<Foo> getFoos() {
        return foos;
    }
}

Unmarshalling:

FooContainer container = new FooContainer(Arrays.asList(new Bar(), new Baz()));

JAXBContext jaxbContext = JAXBContext.newInstance(FooContainer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(container, System.out);

Upvotes: 1

Related Questions