0
我想创建一个工具方法,有两个对象作为参数,并从中执法官一个XML。下面的代码工作,如果我使用实际的对象类型为from参数,但我如何使这种通用?下面不会编译,因为它无法解析从对象到类型。有任何想法吗?如何使Object.class类型通用
public static String getXML(Object from){
StringWriter xml = new StringWriter();
try {
JAXBContext.newInstance(from.class).createMarshaller().marshal(from, xml);
} catch (Exception e) {
e.printStackTrace();
}
return xml.toString();
}
您正在寻找的'Class'类?你可以做'from.getClass()'。这只会在'to'是同一类型时才起作用。 –
@SotiriosDelimanolis对不起错字在我的代码,现在固定......请添加您的评论作为一个答案,所以我可以接受 – c12
什么您已经发布,现在肯定是不对的。你需要'from.getClass()'。 –