Yeah, I know it's an old problem and all are tired of this one, but it's still newsgroups' hit. Sometimes XSLT is the off-shelf solution (not really perf-friendly though), but <xsl:output indent="yes"/> is just ignored in MSXML. In .NET one can leverage XmlTextWriter's formatting capabilities, but what in MSXML? Well, as apparently many forgot MSXML implements SAX2 and includes MXXMLWriter class, which implements XML pretty-printing and is also SAX ContentHandler, so can handle SAXXMLReader's events. That's all needed to pretty-print XML document in a pretty streaming way:
<html> <head> <title>MXXMLWriter sample.</title> <script type="text/javascript"> var reader = new ActiveXObject("Msxml2.SAXXMLReader.4.0"); var writer = new ActiveXObject("Msxml2.MXXMLWriter.4.0"); writer.indent = true; writer.standalone = true; reader.contentHandler = writer; reader.putProperty("http://xml.org/sax/properties/lexical-handler", writer); reader.parseURL("source.xml"); alert(writer.output); </script> </head> <body> <p>MXXMLWriter sample.</p> </body> </html>
chrisp, no, I don't think you can create content handler in javascript. Why do you need it?
Oleg, that seems cool to use a writer as a content handler. Do you know any way I can provide my own from JavaScript? I tried reader.contentHandler = this but got a type mis-match, so I guess I can't implement the interface as functions in my JavaScript.
Thanks, Reinier!
Indenter given loses all comments present in the original XML document. To preserve comments the ISAXLexicalHandler interface of the writer can be used. Insert the following before parsing
reader.putProperty("http://xml.org/sax/properties/lexical-handler", writer);
Oh, I knew it, I remeber it worked for me, but was too lazy to find out when it does work. Thanks!
Still for just indenting, DOM and XSLT is overkill.
Oleg, you are not right about MSXML. It does ignore indent="yes" if you use string or stream output. However, if you use transformNodeToObject (or I suppose if you assign XmlDocument to output property) it handles indent attribute.