public static String dumpModelAsXml ( Object definition , ClassLoader classLoader , boolean includeEndTag , int indent ) throws JAXBException , XMLStreamException { JAXBContext jaxbContext = JAXBContext . newInstance ( JAXB_CONTEXT_PACKAGES , classLoader ) ; StringWriter buffer = new StringWriter ( ) ; // we do not want to output namespace XMLStreamWriter delegate = XMLOutputFactory . newInstance ( ) . createXMLStreamWriter ( buffer ) ; JaxbNoNamespaceWriter writer = new JaxbNoNamespaceWriter ( delegate , indent ) ; // we do not want to include the customId attribute writer . setSkipAttributes ( "customId" ) ; Marshaller marshaller = jaxbContext . createMarshaller ( ) ; marshaller . setProperty ( Marshaller . JAXB_SCHEMA_LOCATION , "" ) ; marshaller . setProperty ( Marshaller . JAXB_FRAGMENT , Boolean . TRUE ) ; marshaller . setProperty ( Marshaller . JAXB_FORMATTED_OUTPUT , Boolean . TRUE ) ; marshaller . marshal ( definition , writer ) ; String answer = buffer . toString ( ) ; // we can collapse <method> <tokenize> and <xtokenize> as they do not include a value answer = collapseNode ( answer , "method" ) ; answer = collapseNode ( answer , "tokenize" ) ; answer = collapseNode ( answer , "xtokenize" ) ; // if there is only 1 element them collapse it, eg <log xxx></log> => <log xxx/> if ( writer . getElements ( ) == 1 ) { String token = "></" + writer . getRootElementName ( ) + ">" ; answer = answer . replaceFirst ( token , "/>" ) ; } if ( ! includeEndTag ) { // remove last end tag int pos = answer . indexOf ( "</" + writer . getRootElementName ( ) + ">" ) ; if ( pos != - 1 ) { answer = answer . substring ( 0 , pos ) ; } // and trim leading/ending spaces/newlines answer = answer . trim ( ) ; } return answer ; }
Dumps the definition as XML