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: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds