In my posts Implementing IXmlWriter Series, I wrote a streaming XML writing class whose interface is based on .NET’s XmlWriter. I recently discovered that MSXML provides its own method to write streaming XML through the class MXXMLWriter.
MXXMLWriter supports a large set of functionality including encoding, indentation, disabling output escaping, and writing XML fragments. The generated XML can be written to an IStream, a BSTR, or a DOMDocument object. However, it’s interface leaves much to be desired. Usage looks like this:
-
#import <msxml3.dll>
-
-
…
-
-
// I’m using the #import-generated _com_ptr_t-based smart pointers
-
MSXML2::IMXWriterPtr spMXWriter;
-
hr = spMXWriter.CreateInstance(__uuidof(MSXML2::MXXMLWriter30));
-
_ASSERT(SUCCEEDED(hr)); // TODO
-
-
// Configure the IMXWriter as appropriate. We will be using the default of
-
// writing to a BSTR which can be retrieved using spMXWriter->get_output().
-
-
MSXML2::ISAXContentHandlerPtr spSAXContentHandler(spMXWriter);
-
_ASSERT(spSAXContentHandler != NULL); // TODO
-
-
// Be sure to check the hrs below
-
-
hr = spSAXContentHandler->startDocument();
-
hr = spSAXContentHandler->startElement(L"", 0, L"root", 4, L"root", 4, NULL);
-
hr = spSAXContentHandler->characters(L"text", 4);
-
// endElement also takes the element name. This means we may need to
-
// maintain our own open element stack.
-
hr = spSAXContentHandler->endElement(L"", 0, L"root", 4, L"root", 4);
-
hr = spSAXContentHandler->endDocument();
The rough IXmlWriter equivalent is:
-
#include "StringXmlWriter.h"
-
-
…
-
-
StringXmlWriter xw;
-
xw.WriteStartDocument();
-
xw.WriteStartElement("root");
-
xw.WriteString("text");
-
xw.WriteEndElement(); // /root
-
xw.WriteEndDocument();
However, there might be a case to change IXmlWriter to use MXXMLWriter internally.
Recent Comments