両方読んだら
クラスをパラメーターとして渡し、Java で汎用コレクションを返すにはどうすればよいですか?
と
Javaでクラスをパラメータとして渡すにはどうすればよいですか?
オブジェクトをシリアル化するメソッドに、JibX で生成されたクラスをパラメーターとして渡す方法がわかりません。
次のようなことを実現したいと思います。
protected static String SerializeObject( Class clazz , Object request)
{
String message = null;
try
{
IBindingFactory lBindingFactory = BindingDirectory.getFactory(
clazz.class);
IMarshallingContext lContext = lBindingFactory.
createMarshallingContext();
ByteArrayOutputStream lOutputStream = new ByteArrayOutputStream();
lContext.marshalDocument(request, "UTF-8", null,
lOutputStream);
message = new String(lOutputStream.toByteArray(), "UTF-8");
}
catch (JiBXException lEx)
{
throw new RuntimeException("Problems generating XML, " +
"underlying problem is " + lEx.getMessage(), lEx);
}
catch (UnsupportedEncodingException lEx)
{
throw new RuntimeException("Problems generating XML in specified" +
"encoding, underlying problem is " + lEx.getMessage(), lEx);
}
return message;
}
クラスは、BindingDirectory がオブジェクトをシリアル化するために使用するものを定義するためにのみ使用されます。