Unmarshalling XML with Java Generics (collections)

Lukec234

New Member
I need to unmarshal a piece of XML that I get through Yahoo's YQL Webservices.The XML looks like this:\[code\]<?xml version="1.0" encoding="UTF-8"?><query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="8" yahoo:created="2012-07-16T16:43:15Z" yahoo:lang="en-US"><results> <results> <GsearchResultClass>GwebSearch</GsearchResultClass> <unescapedUrl>http://www.test.com/nl/nieuws</unescapedUrl> <url>http://www.test.com/nl/nieuws</url> <visibleUrl>www.test.com</visibleUrl> <cacheUrl>http://www.google.com/search?q=cache:0B4z0dyd94gJ:www.test.com</cacheUrl> <title>Nieuws | test.com</title> <titleNoFormatting>Nieuws | test.com</titleNoFormatting> <content>text text text text text text text text <b>...</b></content> </results> [...]</results></query>\[/code\]The duplicate "results" key is not an error by the way.I'm trying to unmarshal this with the following classes:\[code\]@XmlRootElement(name="query")public class YQL<T> { List<T> results; @XmlElementWrapper(name="results") @XmlElement(name="results") public List<T> getResults() { return results; } public void setResults(List<T> results) { this.results = results; } }@XmlTypepublic class SearchResult{ String url,title,content; @XmlElement public String getUrl() { return url; } public void setUrl(String url) { this.url = url; }[...]public class YQLFeedReaderImpl<T> implements FeedReader<YQL<T>> {private Class<T> resultClazz;[...]final JAXBContext context = JAXBContext.newInstance(YQL.class,resultClazz);final Unmarshaller unmarshaller = context.createUnmarshaller();URL url = new URL([....]);return (YQL<T>) unmarshaller.unmarshal(url);\[/code\]What I'm getting returned for T elements are generic ElementNSImpl in stead of SearchResult.Any suggestions on how to deal with this?Cheers,Marc
 
Back
Top