JAXB使用@XmlMixed Annotation进行解组

时间:2014-04-14 10:25:18

标签: java xml jaxb annotations unmarshalling

解组XML文档时遇到问题。我在这里找到了很好的提示(“JAXB- @XmlMixed usage for reading @XmlValue and @XmlElement”),但我无法在我的代码中采用这一点。

首先..这里是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>

在内容块中,可能有一个文本或多个具有属性的属性组(从不同时使用!)。遗憾的是,这是一个给定的xml结构,我无法改变它。

我尝试使用以下类结构,但我的XmlMixed注释中必定存在错误。我甚至不知道我是否必须在这里使用@XmlMixed或者是否还有其他更好的解决方案!?

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

0 个答案:

没有答案