Coldfusion - XML Pretty Print
There are a lot of ways to pretty print XML, but I have yet to find one using 开发者_StackOverflow中文版a ColdFusion function.
This is a common question, but again I want to do this within ColdFusion.
A quick search on http://cflib.org turned up xmlIndent().
<pre>#xmlIndent(xmlString)#</pre>
see top answer of: 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
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>
Use prettydiff.com/markup_beauty.js. If it is capable of supporting invalid markup, fragments, and JSTL code then it should be able to handle CFML without any burden. Consider the following example of a complex JSTL tag.
<c:out value="<strong>text</strong>"/>
You can demo that application using a web tool at prettydiff.com. Just choose the "beautify" and "markup" options.
精彩评论