Below is the xml I have to work with:
<watermarks identifier="X" hostname="X">
<watermark type="XX" value="1234"/>
<watermark type="YY" value="1234" />
</watermarks>
I just want to get a list of Watermark objects using JAXB without creating a new Watermarks class. Is it possible or should I have to create a Watermarks class which contains a list of Watermark objects ?
Thanks.
You could use a StAX (JSR-173)
XMLStreamReaderto parse the XML document (an implementation is included in the JDK/JRE since Java SE 6). Then you could advance it to advance to eachwatermarkelement and then have JAXB (JSR-222) unmarshal that.Demo
Assuming the
Watermarkclass is annotated with@XmlRootElementyou could do the following.Full Example
Generic List Wrapper Class
In my answer to the question below I provided an example of a generic list wrapper class that you may find useful.