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.