From some library, I’m getting such an input string:
<link>
<name>button1</name>
<target>there</target>
</link>
<link>
<name>button2</name>
<target>there2</target>
</link>
(note that this is not an XML document as it has no root) and I have this class:
@XmlRootElement(name = "link")
public class TableTagLinkElement {
private String name;
private String target;
// getters and setters
}
How can I unmarshall this easily into a list of TableTagLinkElements, in a generic way such that I could implement such a method:
public <T> List<T> parseCollection(String xmlString, Class<T> rootClass)
i.e., without any previous knowledge of the TableTagLinkElement class or of the name of the <link> tag?
I know about solutions that create a wrapper class with a list, but I think they’re not applicable here, are they?
If you can wrap a
<root>...</root>element around the complete XML string, then you could create anXMLStreamReaderreading from that string, then loop over the reader, unmarshalling eachlinkas you go. For example (exception handling omitted)