本质上,我想做的是创建一个编组器,它可以接受我给它的任何类对象,例如汽车对象或人对象,并且它必须返回 XML 字符串。
这是我到目前为止所得到的:
public <T> String CreateXML(T objToSerialize)
{
String xml = "";
try
{
JAXBContext context = JAXBContext.newInstance(objToSerialize.getClass());
Marshaller marshaler = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaler.marshal(objToSerialize.getClass(),writer);
xml = writer.toString();
System.out.println(xml);
return xml;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return xml;
}
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
警告:发生了非法反射访问操作