Alexander Kleinhans
Alexander Kleinhans

Reputation: 6248

JAXB @XmlValue and @XmlElements

Is it possible to have a JAXB element that renders with a value AND elements?

I'm trying to render something like:

<thing>
    <otherthing></otherthing>
    This is some text
</thing>

It's know probably not even valid XML, but unfortunatley, what I'm trying to render requires it and is not optional.

Having a value and elements give's a IllegalAnnotationExceptions.

Upvotes: 1

Views: 884

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66714

What you have shown is completely valid XML. An element has has text() and element children is known as mixed content.

Use the @XmlMixed JAXB annotation instead of @XmlValue to indicate that the element is mixed content.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Thing {

  private List<Object> mixedContent = new ArrayList<Object>();

  @XmlElementRef(name="thing", type=Thing.class)
  @XmlMixed
  public List<Object> getMixedContent() {
    return mixedContent;
  }

  public void setMixedContent(List<Object> mixedContent) {
     this.mixedContent = mixedContent;
  }

}

Upvotes: 1

Related Questions