Beispiel #1
0
 /**
  * 转为xml串
  *
  * @param obj
  * @return
  */
 public String toXML(Object obj) {
   String result = null;
   try {
     JAXBContext context = JAXBContext.newInstance(obj.getClass());
     Marshaller m = context.createMarshaller();
     m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
     m.setProperty(Marshaller.JAXB_FRAGMENT, true); // 去掉报文头
     OutputStream os = new ByteOutputStream();
     XMLSerializer serializer = getXMLSerializer(os);
     m.marshal(obj, serializer.asContentHandler());
     result = os.toString();
   } catch (Exception e) {
     e.printStackTrace();
   }
   Logger.info("response text:" + result);
   return result;
 }