0

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...
    }
Community
  • 1
  • 1
  • What is the error you are getting ? – Shirishkumar Bari Apr 14 '14 at 10:28
  • Sorry... i am getting this exception: 1 counts of IllegalAnnotationExceptions Invalid @XmlElementRef : Type "class Attribute" or any of its subclasses are not known to this context. This is wired.. because the Attribute.class is not part of the XmlElementRef. – user3531379 Apr 14 '14 at 10:37
  • I think it might be an issue declaring static class . may be Attribute is not initialized when trying to initialize the AttributeGroup class – Shirishkumar Bari Apr 14 '14 at 11:24
  • 1
    Adding @XmlRootElement at class Attribute and AttributeGroup seems to fix the exception, but the parsed lists of attributes are still empty. – user3531379 Apr 15 '14 at 08:47

0 Answers0