我有以下方法:转换非通用方法通用方法
private <U> void fun(U u) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(u.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(u, System.out);
}
编组()方法采用不同类型的参数。见here 这需要:
- 的ContentHandler
- 的OutputStream
- XMLEventWriter的
- XMLStreamWriter等
如何修改上述方法,使得代替System.out
,我可以通过目的地在函数参数中进行编组。
例如,我想调用如下方法:
objToXml(obj1,System.out);
outToXml(obj1,file_pointer);
同样。
我试着用fun(obj1,PrintStream.class,System.out)
以下,但它是unsuccessfull:
private <T, U, V> void fun(T t, Class<U> u, V v) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(t.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(t, (U) v);
}
类型'U'在第一个代码完全没有必要;你可以使用'Object' – newacct 2012-08-09 19:23:43