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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds