views:

49

answers:

3

There are a lot of ways to pretty print XML, but I have yet to find one using a ColdFusion function.

This is a common question, but again I want to do this within ColdFusion.

+1  A: 

see top answer of: http://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript. Try it with XmlTransform(). If it doesn't work, pick a Java XSLT engine like http://saxon.sourceforge.net/ as suggested

Henry
+2  A: 

A quick search on http://cflib.org turned up xmlIndent().

<pre>#xmlIndent(xmlString)#</pre>
Ben Doom
It's says the stuff at cflib.org is 'open source', but this can mean different things to different people. Is there a license they use, or is it free as in free for both commercial and and non-commercial use?
Skolem
My understanding is that it is free as in both beer and speech. If you are concerned, you can contact the author from the UDF page.
Ben Doom
A: 

I use a Java solution XOM for this purpose, so you'd need to have its jar in your classpath for it work. Following answer in original Java code:

<cfset xmlString = [your xml here]/>

<cfscript>
encoding = "ISO-8859-1";
parser = createObject("java", "nu.xom.Builder").init();
doc = parser.build(xmlString);
out = createObject("java", "java.io.ByteArrayOutputStream").init();
Serializer serializer = new Serializer(out, encoding);
// bunch of options
serializer.setIndent(4);
serializer.setMaxLength(64);
serializer.setPreserveBaseURI(true);

serializer.write(doc);
serializer.flush();
</cfscript>

<cfoutput>#out.toString(encoding)#</cfoutput>
orangepips