I had a problem parsing an XML document. Here I found some good hints (" JAXB- @XmlMixed use for reading @XmlValue and @XmlElement ), but I was not able to accept this in my code.
First ... here is an xml example:
<test>
<content type="text">This is a content text</content>
<content type="attributes">
<attributeGroup name="group1">
<attribute name="attr1">value1</attribute>
</attributeGroup>
<attributeGroup name="group2">
<attribute name="attr2">value2</attribute>
<attribute name="attr3">value3</attribute>
</attributeGroup>
</content>
</test>
A content block can contain either text or several attribute groups with attributes (never both!). Unfortunately, this is a given xml structure, and I cannot change it.
I tried it with the following class structures, but there should be an error in my XmlMixed annotation. I don’t even know if I should use @XmlMixed here or if there is another better solution !?
public static class Content {
@XmlMixed
private List<String> text;
@XmlElementRef(name = "attributeGroup", type = AttributeGroup.class)
private List<AttributeGroup> attributeGroups;
}
public static class AttributeGroup {
@XmlAttribute(name="name")
private String name;
@XmlElement(name="attribute", type=Attribute.class)
private List<Attribute> attributes;
}
public static class Attribute {
@XmlAttribute(name="name")
private String name;
@XmlValue
private String value;
}