Exporting Data as an XML File

Environment: Tested on VC6 SP4 and VC7

Introduction

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:

Data

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

XML

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

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:

void
ExportBooks()
{

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.

Downloads

Download demo project - 33 Kb
Download source - 3 Kb


Comments

  • 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?

    Thanks
    D

    Reply
  • 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("'");

    csNewData.Replace(csToFind, csToReplace);
    }

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

    csNewData.Replace(csToFind, csToReplace);
    }
    }
    }

    Reply
  • 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.

    thx

    Reply
  • 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.

    Reply
  • 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 � ?
    Like

    <book>
    n� five
    </book>

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

    Reply
  • Importing !!!

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

    Originally posted by: Pavan Kumar Reddy

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

    Reply
  • 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!!!

    Reply
  • Huh?

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

    Originally posted by: John T. Edwards

    You have got to be kidding.

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • 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