# dom4j OutputFormat



## swaters (Jul 21, 2003)

I am working on a Java program which takes an XML string and formats it to look nice (PrettyPrint). My only problem is, there are multi-line comments in the XML, and the formatter takes everything between the open and close XML comment tags () and puts it onto one line. I've been looking through the API (http://www.dom4j.org/apidocs/org/dom4j/io/OutputFormat.html) but haven't been able to find anything which will supress this beahavior. Here is my function:

public static String xmlFormat( Document document ) throws ASExceptionUtl {
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setIndent( "\t" );
format.setLineSeparator( "\r\n" );
format.setSuppressDeclaration( true );
format.setExpandEmptyElements( false );
StringWriter stringWriter = new StringWriter();
XMLWriter writer = new XMLWriter( stringWriter, format );
writer.write( document );
writer.close();
return stringWriter.toString().trim();
}
catch( IOException exception ) {
throw new ASExceptionUtl( "IOException", exception );
}
}

I thought the problem might be with the StringWriter and XMLWriter classes, but I can't access them in the API online (keep getting timeouts).

Any ideas? Thanks.


----------



## techshar31 (Nov 10, 2005)

I know I've run into this problem but it was at least two years ago. Have you tried "pre-processing" to process and remove the comments? Or, if you want to include the comments I *think* you could arrange some sort of a loop and filereader to do that. I'm not sure that the API will ever handle those comments the way you would like it to?


----------

