私は次の方法を持っています:
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);
}
marshal()メソッドは、さまざまなタイプの引数を取ります。ここを参照してください 。
- ContentHandler
- OutputStream
- XmlEventWriter
- XMLStreamWriterなど。
System.out
の代わりに、関数の引数でマーシャリングの宛先を渡すことができるように、上記のメソッドを変更する方法。
たとえば、次のようなメソッドを呼び出します。
objToXml(obj1,System.out);
outToXml(obj1,file_pointer);
同じく。
でフォローしようとしましfun(obj1,PrintStream.class,System.out)
たが、失敗しました:
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);
}