3

my question is: from the xml scheme :

<topnode>
    topNodeValue
   <bottomnode/> 
</topnode>

generated class with Jaxb looks like

class topnode {
    List<bottomnode> bottomnodeList;
}

Which does not generate the value field to set value for topnode.

How can I acheive this? Thanks.

Emond
  • 50,210
  • 11
  • 84
  • 115
Tammy
  • 123
  • 1
  • 7

2 Answers2

3

When the contents of an element contain both character and element data it is called mixed content. In JAXB (JSR-222) this is mapped with the @XmlMixed annotation like:

class topnode {
    @XmlMixed
    String text;

    List<bottomnode> bottomnodeList;
}

The use of mixed content can be tricky, since you may get unexpected results due to text nodes used for formatting. For a more detailed explanation see the following answer to a similar question.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

For text nodes, use @XmlValue annotation. Something like this:

class topnode {

    @XmlValue
    String topNodeValue;

    List<bottomnode> bottomnodeList;

}

As a suggestion, try to respect java naming standards and if it doesn't match the xml elements use the name attribute of the @Xml... annotations.

tibtof
  • 7,857
  • 1
  • 32
  • 49
  • since the `bottomnodeList` property maps to an XML element you will get an exception if you try to use `@XmlElement` instead. The OP is trying to map mixed content, so `@XmlMixed` should be used instead of `@XmlValue`. The following should help: http://stackoverflow.com/a/11099303/383861 – bdoughan Aug 30 '12 at 13:49
  • 1
    @BlaiseDoughan Thanks! I didn't know about `@XmlMixed`, since I never needed it. I think you should put this comment as answer, you'll definitely have an up from me. – tibtof Aug 30 '12 at 13:57