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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds