Implementing IXmlWriter Part 1: The Basics

This is part 1 of my Implementing IXmlWriter post series.

After writing my blog post Don’t Form XML Using String Concatenation, I realized that writing a C++ System.Xml.XmlWriter workalike involves some interesting challenges. Therefore, I’ve decided to write a series of blog posts about building a streaming C++ XML generator, a.k.a. IXmlWriter, step-by-step. For this series, I will follow the practice of test-driven development and write a test case followed by an implementation which passes the test case. Future posts’ test cases will be constructed to illustrate bugs in or new features desired from the previous post’s implementation. The test cases will be constructed with the goal of having IXmlWriter be as similar to System.Xml.XmlWriter as possible.

This is the first post in that series. Today’s test case is:

StringXmlWriter xmlWriter;


std::string strXML = xmlWriter.GetXmlString();
// strXML should be <root><element>Contents</element></root>

Looking carefully at this test case, I see the following requirements for StringXmlWriter:

  1. It needs to support 4 functions: WriteStartElement(), WriteEndElement(), WriteString(), and GetXmlString().
  2. It must provide the ability to retrieve the XML as a std::string. For simplicity, I will simply keep the generated XML as a std::string member variable and return it from GetXmlString().
  3. It needs to keep track of what XML elements have been opened so that it can properly produce the end tag when WriteEndElement() is called. It is sensible for StringXmlWriter to contain this logic and not the caller (in other words, WriteEndElement() shouldn’t take the name of the element to close) because it doesn’t make sense for WriteEndElement() to close any other element other than the most recently opened one. The natural data structure to store this information is a stack, so I will use a std::stack.

    Remember, I am only concerned with passing this test case. To fix any bugs or deficiencies I must write a new test case first, and then fix the implementation accordingly. Keeping this in mind, here is the overly simple implementation which I came up with to pass this test case:

    class StringXmlWriter
        std::stack<std::string> m_openedElements;
        std::string m_xmlStr;
        void WriteStartElement(const std::string& localName)
            m_xmlStr += ‘<’;
            m_xmlStr += localName;
            m_xmlStr += ‘>’;
        void WriteEndElement()
            std::string lastOpenedElement =;
            m_xmlStr += “</”;
            m_xmlStr += lastOpenedElement;
            m_xmlStr += ‘>’;
        void WriteString(const std::string& value)
            m_xmlStr += value;
        std::string GetXmlString() const
            return m_xmlStr;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s