67,513
社区成员
发帖
与我相关
我的任务
分享
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class JaxbContextUtil {
private static JAXBContext context =null;
private static Marshaller m = null;
//将对象转化为xml
public static String objectToXml(Object obj) {
StringWriter sw = new StringWriter();
try {
context = JAXBContext.newInstance(Object.getClass());
m = context.createMarshaller();
m.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
m.marshal(obj , sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
}