//输出
StringWriter sw = new StringWriter();
//转换类
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化输出的格式
marshaller.setProperty(Marshaller.JAXB_FRAGNENT,true);//去掉xml默认生成的报文头
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);//设置xml格式
marshaller.setProperty(Marshaller.JAXB_ENCODING,"GBK");//设置编码格式
marshaller.marshal(obj, sw);