Exporting Data as an XML File | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 1, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Advertisement

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.