On pretty-printing XML documents using MSXML

| 6 Comments | 4 TrackBacks

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:

      <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);
      <p>MXXMLWriter sample.</p>

Related Blog Posts

4 TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/329

Pretty Printing XML using SAX from IGather Info - Eden Ridgway's Blog on November 30, 2005 7:30 AM

Pretty Printing XML using SAX Read More

Pretty Printing XML using SAX Read More

Pretty Printing XML using SAX Read More


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.

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.

Leave a comment