JAXB unmarshalling with @XmlMixed Annotation

╄→尐↘猪︶ㄣ 提交于 2020-01-14 06:07:34

问题


I have a problem while unmarshalling an XML document. I found good hints here ("JAXB- @XmlMixed usage for reading @XmlValue and @XmlElement") but I wasn't able to adopt this on my code.

First.. here is an example of the xml:

    <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>

Within the content block there could be either a text or several attribute groups with attributes (never both!). Sadly this is a given xml structure and I can't change it.

I tried it with the following class structures but there must be an error within my XmlMixed annotation. I even don't know if I must use the @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;
      // getter+setter...
    }

    public static class AttributeGroup {
      @XmlAttribute(name="name")
      private String name;
      @XmlElement(name="attribute", type=Attribute.class)
      private List<Attribute> attributes;
      // getter+setter...
    }

    public static class Attribute {
      @XmlAttribute(name="name")
      private String name;
      @XmlValue
      private String value;
      // getter+setter...
    }

来源:https://stackoverflow.com/questions/23057726/jaxb-unmarshalling-with-xmlmixed-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!