2

How can I generate the following structure using JAXB? In my case I have a dynamic properties list which may have 3 types: integer, string or list. However, list properties have nested elements. How can I achieve it?

<settings>
    <setting type=”string” name="username">user1</setting>
    <setting type=”integer” name="age">25</setting>
    <setting type=”list” name="list">
        <setting type=”string”>foo</setting>
        <setting type=”string”>bar</setting>
    </setting>
</settings>
Rui
  • 5,900
  • 10
  • 38
  • 56
  • 1
    What would you like your class to look like? All the setting objects in one collection or separated by type? – bdoughan May 17 '13 at 12:09

1 Answers1

0

The best way would be to generate an XSD for your XML format. Then you run jaxb to generate code against the XSD. You could hand-create JAXB-annotated classes, but the XSD is likely easier, and serves multiple (valuable) purposes. Not the least of which is it will help you understand if you're data format makes sense. For example, it sounds like you have string, int, or list? In that case your XSD would have a choice element. And the auto-generated jaxb code would handle that. Writing the XSD will help ensure your data format makes sense. And it can be used to validate input matches your required format. And you generate jaxb code from it.

Here's a link on using xjc to generate classes from jaxb: How to generate JAXB classes from XSD?

Community
  • 1
  • 1
user1676075
  • 3,056
  • 1
  • 19
  • 26