This is part 1/14 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:
|
|
Looking carefully at this test case, I see the following requirements for StringXmlWriter
:
- It needs to support 4 functions:
WriteStartElement()
,WriteEndElement()
,WriteString()
, andGetXmlString()
. - It must provide the ability to retrieve the XML as a
std::string
. For simplicity, I will simply keep the generated XML as astd::string
member variable and return it fromGetXmlString()
. - 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 forStringXmlWriter
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 forWriteEndElement()
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 astd::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:
|
|