Ixmlwriter

Implementing IXmlWriter Part 4: Collapsing Empty Elements
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-10
Implementing IXmlWriter Part 4: Collapsing Empty Elements

This is part 4/14 of my Implementing IXmlWriter post series.

One of the enhancements that XML introduced over SGML was a shorthand for specifying an element with no content by adding a trailing slash at the end of an open element. For example, <br/> is equivalent to <br></br>. Let’s add this functionality to the previous version of IXmlWriter.

Here’s the test case:

1
2
3
4
5
6
7
8
StringXmlWriter xmlWriter;
xmlWriter.WriteStartElement("root");
  xmlWriter.WriteStartElement("emptyElement");
  xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();

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

How does this affect our previous implementation?

Read more...
Implementing IXmlWriter Part 3: Supporting WriteElementString()
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-07
Implementing IXmlWriter Part 3: Supporting WriteElementString()

This is part 3/14 of my Implementing IXmlWriter post series.

Today’s addition to the previous iteration of IXmlWriter is quite trivial: supporting the WriteElementString() method.

Here’s the test case:

1
2
3
4
5
6
7
StringXmlWriter xmlWriter;
xmlWriter.WriteStartElement("root");
  xmlWriter.WriteElementString("element", "value");
xmlWriter.WriteEndElement();

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

Implementation is extremely simple because WriteElementString() is nothing but a convenience method which calls WriteStartElement(), WriteString(), and WriteEndElement(). Therefore, here’s the new StringXmlWriter:

Read more...
Implementing IXmlWriter Part 2: Escaping Element Content
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-06
Implementing IXmlWriter Part 2: Escaping Element Content

This is part 2/14 of my Implementing IXmlWriter post series.

In the previous post of this series, we ended up with a simple class which could write XML elements and element content to a std::string. However, this code has a common, serious problem that was mentioned in my post Don’t Form XML Using String Concatenation: it doesn’t properly escape XML special characters such as & and <. This means that if you call WriteString() with one of these characters, your generated XML will be invalid and will not be able to be parsed by an XML parser.

Read more...
Implementing IXmlWriter Part 1: The Basics
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-09-30
Implementing IXmlWriter Part 1: The Basics

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.

Read more...