【发布时间】:2016-05-31 19:07:51
【问题描述】:
我有一些带有 POJO 的包用于解组。我想创建一个通用方法,您可以在其中传递您将解组到的类。
例如:
public class Test<E>
{
E obj;
// Get all the tags/values from the XML
public void unmarshalXML(String xmlString) {
//SomeClass someClass;
JAXBContext jaxbContext;
Unmarshaller unmarshaller;
StringReader reader;
try {
jaxbContext = JAXBContext.newInstance(E.class); // This line doesn't work
unmarshaller = jaxbContext.createUnmarshaller();
reader = new StringReader(xmlString);
obj = (E) unmarshaller.unmarshal(reader);
} catch(Exception e) {
e.printStackTrace();
}
}
}
我在上面代码中指出的行上遇到错误:Illegal class literal for the type parameter E。 E 当然会来自实际存在的 POJO 列表。
我将如何做到这一点?
【问题讨论】:
标签: java xml generics unmarshalling