@SuppressWarnings("unchecked")
public static String marshal(Object obj) {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
XmlRootElement xmlRootAnnotation = obj.getClass().getAnnotation(XmlRootElement.class);
System.out.println(xmlRootAnnotation);
if (xmlRootAnnotation == null) {
XmlType xmlTypeAnnotation = obj.getClass().getAnnotation(XmlType.class);
QName qname = new QName("", xmlTypeAnnotation.name());
JAXBElement<Object> jaxbElement = new JAXBElement<Object>(qname, (Class<Object>) obj.getClass(), null, obj);
jaxbMarshaller.marshal(jaxbElement, stringWriter);
} else {
jaxbMarshaller.marshal(obj, stringWriter);
}
} catch (Exception e) {
e.printStackTrace();
}
return stringWriter.toString();
}
MarshalXML.java 文件源码
java
阅读 23
收藏 0
点赞 0
评论 0
项目:soapbox-race-core
作者:
评论列表
文章目录