Exporting Data as an XML File


Application Security Testing: An Integral Part of DevOps

Environment: Tested on VC6 SP4 and VC7


On a project, I had the task of exporting data from an application as XML text. I collected as much information about XML as I being could and trained myself on the topic. When I was familiar with XML, I thought of a way to export the data as XML. In addition, I programmed this class. In principle, the class functions as follows:

First, you have to fill the class with the data you want to export. Do this with AddTag(). For example:

xml.AddTag (NULL,"Book-Review");

Every AddTag() returns a handle of the type POSITION that can be used as the parent tag for others. If all tags are added, you call only Write(). That's it.

Here is an example for the output. It uses the class to export the information of a book as follows:


Title: Catcher in the Rye
Author: J.D. Salinger
Publisher: Little Brown and Company
Pub_Date: 1951


<?xml version = "1.0" encoding = "utf-8" standalone = "yes"?>
        <Title>Catcher in the Rye</Title>
        <Author>J.D. Salinger</Author>
        <Publisher>Little Brown and Company</Publisher>

Program Code

void ExportBook()
POSITION parent;
CXMLWriter xml;

parent = xml.AddTag (NULL,"Book-Review"); // First Tag has no
                                          // parent; therefore,
                                          // we set it to NULL
parent = xml.AddTag (parent,"Book");      // this tag is a sub-tag
                                          // from "Book-Review"
xml.AddTag (parent,"Title","Catcher in the Rye");
xml.AddTag (parent,"Author","J.D. Salinger");
xml.AddTag (parent,"Publisher","Little Brown and Company");
xml.AddTag (parent,"Pub_Date","1951");

xml.Write ("C:\\demoXML.xml");

The following code demonstrates how to export the data of two books:


POSITION parent, review;
CXMLWriter xml;

review = xml.AddTag (NULL,"Book-Review"); // First Tag has no
                                          // parent; therefore,
                                          // we set it to NULL
// add first book
parent = xml.AddTag (review,"Book");      // this tag is a sub-tag
                                          // from "Book-Review"
xml.AddTag (parent,"Title","Catcher in the Rye");
xml.AddTag (parent,"Author","J.D. Salinger");
xml.AddTag (parent,"Publisher","Little Brown and Company");
xml.AddTag (parent,"Pub_Date","1951");
// add second book
parent = xml.AddTag (review,"Book");      // this tag is a sub-tag
                                          // from "Book-Review"
xml.AddTag (parent,"Title","Franny and Zooey");
xml.AddTag (parent,"Author","J.D. Salinger");
xml.AddTag (parent,"Publisher","Little Brown and Company");
xml.AddTag (parent,"Pub_Date","1961");

xml.Write ("C:\\demoXML.xml");


If a tag has attributes, you use AddAttribute() after adding the tag. For example, we have on the tag "Title" the attribute "ISBN"; use the following:

POSITION title = xml.AddTag (parent,"Title","Catcher in the Rye");
xml.AddAttribute (title,"ISBN","0316769533");

If you want a link to a DTD in your output, use SetDTD() to set it.

Error Handling

If an error occurs in a function, a CXMLException is thrown. You can use the member strErrorText for getting information about the error. See the demo program for an example of using it.


Download demo project - 33 Kb
Download source - 3 Kb


  • Handling Large Files

    Posted by Legacy on 12/01/2003 12:00am

    Originally posted by: D

    Do you support the use of large files.

    It seems that it just hangs if you try and use this class on a large data set.

    Any tips?


  • Handling Special Characters

    Posted by Legacy on 09/03/2003 12:00am

    Originally posted by: Douglas Fraser

    You can handle special characters like & < > ' or " by adding the following code

    In function AddTag change the code at the top of the function to do this

    //Scan tag Value for illegal literal character
    CString newTagValue;

    if (szTagValue != NULL)
    GetValidWriteData(szTagValue, newTagValue);
    szTagValue = newTagValue;

    And insert a new function to handle special characters as you want

    void CXMLWriter::GetValidWriteData(const CString & csData, CString& csNewData)
    int psn = csData.Find('&');
    int psn2 = csData.Find('<');
    int psn3 = csData.Find('<');
    int psn4 = csData.Find('\'');
    int psn5 = csData.Find('"');

    csNewData = csData;

    if(psn != -1 || psn2 != -1 || psn3 != -1 || psn4 != -1 || psn5 != -1)
    if(psn != -1)
    CString csToFind("&");
    CString csToReplace("&");

    csNewData.Replace(csToFind, csToReplace);

    if(psn2 != -1)
    CString csToFind("<");
    CString csToReplace("<");

    csNewData.Replace(csToFind, csToReplace);

    if(psn3 != -1)
    CString csToFind(">");
    CString csToReplace(">");

    csNewData.Replace(csToFind, csToReplace);

    if(psn4 != -1)
    CString csToFind("\'");
    CString csToReplace("&apos");

    csNewData.Replace(csToFind, csToReplace);

    if(psn5 != -1)
    CString csToFind("\"");
    CString csToReplace(""");

    csNewData.Replace(csToFind, csToReplace);

  • load xml

    Posted by Legacy on 02/11/2003 12:00am

    Originally posted by: Andreas Groll

    i am searching for a class, that imports/loads data from a xml file into system variables, all xml parsers on the net seem to be really confusing to me. it would be nice if anyone could suggest me an option to do this.


  • Nice job!

    Posted by Legacy on 01/31/2003 12:00am

    Originally posted by: Nadjib KRIM

    I've been looking for a simple class to create an XML file.
    So you did it! :)
    It is clean and easy to use.
    You saved me a lot of time (no, your work is not a waste of time!).

    Thank you.

  • invalide caracters like % or �

    Posted by Legacy on 01/06/2003 12:00am

    Originally posted by: ruicosta

    What about if I use invalide caracters like % or � ?

    n� five

    Ok, the writter works well, but I'm gonna read it, it won't work

  • Importing !!!

    Posted by Legacy on 09/27/2002 12:00am

    Originally posted by: Pavan Kumar Reddy

    Does anybody support a feature for importing of data from an xml file.
    - Pavan -

  • What is it for?

    Posted by Legacy on 09/27/2002 12:00am

    Originally posted by: wegiel

    One just need to download libXML and that's it,no need for writing your own classes.You're waisting your time!!!

  • Huh?

    Posted by Legacy on 09/26/2002 12:00am

    Originally posted by: John T. Edwards

    You have got to be kidding.

  • You must have javascript enabled in order to post comments.

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.