Creating XML files from C++ programs

Environment: Win32, MSVC 6.0, STL

Sometimes it is necessary to create XML files from a program in C++. It is possible to use the very powerful set of IXMLDOMxxx interfaces, but this may add unnecessary requirements and overhead to the program. On the other hand, to create XML files from code is not a trivial task in the general case. Even when simple XML files are created, a set of wrapping classes can still be helpful.

The front-end class CXmlNode exposes a number of constructors to handle the output of different types of variables - strings, wide character strings, numeric values, boolean variables. All constructors take as parameters a reference to an open output stream object and the XML element name. Most of the functionality is provided by the base class, CXmlNodeBase. When the method StartNode is called, the begin tag of the element is created in the output file. The destructor of the class puts automatically the end tag if StartNode has been called.

To attach attributes to an element, it is necessary to construct a CXmlAttributeSet object and call AssignAttributeSet before StartNode is called. This will ensure all attributes are properly put in the begin tag of the element.

Often XML elements are created in fixed order. A set of macros makes this task extremely easy. To create an element with child elements, use the macro bracket pair START_NODE CLOSE_NODE and create all child elements between these macros. The macro DO_NODE creates an element and immediately puts its end tag. The macros with suffix _A take an additional reference to a CXmlAttributeSet object for the attributes of the node.

When more complex processing is required, CXmlNode objects can be manipulated directly or through a smart pointer wrapper, as shown in the sample.

Sample use

 ofstream          ofs("test.xml");
 CXmlAttributeSet  as("a1","v1");

 as.AddAttribute("a2","v2");

 UTL_PutXmlHeader(ofs);

 START_NODE(ofs, "DemoDocument", CXmlNode::EMPTY_DATA);

   START_NODE_A(ofs, "e1", "t1", as);
     DO_NODE(ofs, "e11","t11")
     DO_NODE(ofs, "e12","t12")
     START_NODE(ofs, "e12","t12")
       DO_NODE_A(ofs, "e121","t121", CXmlAttributeSet("wee","jee"))
       DO_NODE(ofs, "answer", 42)
       DO_NODE(ofs, "NO_PI",3.1514)
       START_NODE(ofs, "fact","5  3")
         DO_NODE(ofs, "is ok", true)
       CLOSE_NODE
     CLOSE_NODE
   CLOSE_NODE

   as.AddAttribute("a3","v3");

   DO_NODE_A(ofs, "e2", CXmlNode::EMPTY_DATA, as);

   CXmlNode   *  n3 = new CXmlNode(ofs,"e3",CXmlNode::EMPTY_DATA);
   SmartPtr<CXmlNode>      spXml(NULL);

   n3->StartNode();

   spXml = new CXmlNode(ofs,"e31", "t31");
   spXml->StartNode();
   spXml = NULL;

   spXml = new CXmlNode(ofs,"e32", "&reg;");
   spXml->StartNode();
   spXml = NULL;

   delete n3;

 CLOSE_NODE

The sample does not catch the exceptions which the CXmlNode class can throw. A professional program would handle these exceptions in an appropriate way.

Downloads

Download demo project - 43Kb
Download source - 8Kb



Comments

  • It's OK. Congratulations.

    Posted by Legacy on 11/04/2002 12:00am

    Originally posted by: Tihomir Todorov


    Hi .
    I think it is a good try to read/write from/to Access.

    Reply
  • Tiny error

    Posted by Legacy on 08/08/2002 12:00am

    Originally posted by: Mark Roberts

    Hi,
    
    

    I believe there is an error in function

    CXmlAttributeSet::operator = (CXmlAttributeSet & r);

    If I type:

    CXmlAttributeSet first, second;
    first.AddAttribute( "Jakob", "Hungary");
    second.AddAttribute("Michail", "Russia");
    first = second;

    then 'first' contains both attributes (Jakob and Michail),
    although it should only contain one (Michail).

    All the best from Germany, Mark

    Reply
  • Good job

    Posted by Legacy on 10/11/2001 12:00am

    Originally posted by: Qian

    if do not use std then the code would be better. std is not very fast as I think.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds