Xml

This is part 6 of my Implementing IXmlWriter post series.
Last time’s IXmlWriter has a serious bug: it doesn’t properly handle attribute value escaping and can lead to malformed XML.
Consider the following test case:
StringXmlWriter xmlWriter; xmlWriter.WriteStartElement("root"); xmlWriter.WriteStartElement("element"); xmlWriter.WriteAttributeString("att", "\""); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); std::string strXML = xmlWriter.GetXmlString(); The previous version of IXmlWriter will generate the XML string <root><element att="""/></root>, which is invalid and will be rejected by a XML parser.

This is part 5 of my Implementing IXmlWriter post series.
Today I will add support for writing attributes to yesterday’s version of IXmlWriter.
To learn more about attributes, see the W3C XML 1.0 Recommendation. Writing attributes will be supported with the function WriteAttributeString().
Here’s today’s test case:
StringXmlWriter xmlWriter; xmlWriter.WriteStartElement("root"); xmlWriter.WriteStartElement("element"); xmlWriter.WriteAttributeString("att", "value"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); std::string strXML = xmlWriter.GetXmlString(); // strXML should be <root><element att="value"/></root> Because the changes in Implementing IXmlWriter Part 4 keep start elements unclosed until another function is called which requires them to be closed (e.

This is part 4 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:
StringXmlWriter xmlWriter; xmlWriter.WriteStartElement("root"); xmlWriter.WriteStartElement("emptyElement"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); std::string strXML = xmlWriter.

This is part 2 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.

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.

It seems very common for developers to create XML using string concatenation, as in:
std::string CreateXML ( const std::string& strValue ) { std::string strXML("<tag>"); strXML += strValue; strXML += "</tag>"; return strXML; } As any experienced XML developer knows, this code has a bug: strValue must be escaped (& must be converted to &amp;, < must be converted to &lt;, etc.) or the XML that is generated will not be well-formed and will not be able to be parsed by an XML parser.