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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds