public static String convertToXML ( Object object ) { try { if ( ! JAXB_CONTEXT_MAP . containsKey ( object . getClass ( ) ) ) { JAXB_CONTEXT_MAP . put ( object . getClass ( ) , JAXBContext . newInstance ( object . getClass ( ) ) ) ; } Marshaller marshaller = JAXB_CONTEXT_MAP . get ( object . getClass ( ) ) . createMarshaller ( ) ; marshaller . setProperty ( Marshaller . JAXB_FORMATTED_OUTPUT , true ) ; // 设置CDATA输出字符 marshaller . setProperty ( CharacterEscapeHandler . class . getName ( ) , new CharacterEscapeHandler ( ) { public void escape ( char [ ] ac , int i , int j , boolean flag , Writer writer ) throws IOException { writer . write ( ac , i , j ) ; } } ) ; StringWriter stringWriter = new StringWriter ( ) ; marshaller . marshal ( object , stringWriter ) ; return stringWriter . getBuffer ( ) . toString ( ) ; } catch ( Exception e ) { logger . error ( "" , e ) ; } return null ; }
Object to XML