Xml

Implementing IXmlWriter Part 6: Escaping Attribute Content

Implementing IXmlWriter Part 6: Escaping Attribute Content
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.

Implementing IXmlWriter Part 5: Supporting WriteAttributeString()

Implementing IXmlWriter Part 5: Supporting WriteAttributeString()
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.

Implementing IXmlWriter Part 4: Collapsing Empty Elements

Implementing IXmlWriter Part 4: Collapsing Empty Elements
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.

Implementing IXmlWriter Part 3: Supporting WriteElementString()

Implementing IXmlWriter Part 3: Supporting WriteElementString()
This is part 3 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: 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: classStringXmlWriter { private: std::stack<std::string> m_openedElements; std::string m_xmlStr; public: void WriteStartElement(const std::string& localName) { m_openedElements.

Implementing IXmlWriter Part 2: Escaping Element Content

Implementing IXmlWriter Part 2: Escaping Element Content
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.

Implementing IXmlWriter Part 1: The Basics

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.

Don't Form XML Using String Concatenation

Don't Form XML Using String Concatenation
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.