Click here to Skip to main content
Click here to Skip to main content

XMLWriter - A simple reusable class

By , 20 Mar 2006
 

Introduction

XMLwriter is a simple class that can be used in your application to create/generate XML output. All you need to do is include the xmlwriter.h and xmlwriter.cpp into your project, and enjoy.

The implementation uses only standard CPP libraries and STLs, this can be used for applications in Windows as well as Linux. I have used this in a Windows environment, Cygwin (Linux emulator), and in Linux systems. It works fine in all the three. I hope the same will be the result in other operating systems also.

This XMLWrirter class doesn’t support all the features of XML but surely satisfies the basic needs you will have in most of your applications. You can use this code free of cost, but at your on risk. (Please test the application.)

Features

  1. Easily creates nodes.
  2. Easily creates elements.
  3. Easily adds attributes.
  4. Closes all the open tags with one simple command. (Stack to keep track of all the open tags.)
  5. No need to worry about file operations. (All handled internally.)

Features Missing

  1. No verification and validation is done on the file. (User's responsibility to ensure that file is well formed and valid.)
  2. Right now, provision to add processing instructions and CDATA sections are missing. This can be easily added to the XML class (if you need it).

I have organized the tutorial into three main sections.

Section 1: How to use?

Usage is very simple and easy.

Windows users

  1. Create a simple project in the IDE that you are using.
  2. Add the files xmlwriter.cpp and xmlwriter.h.
  3. Create a main application.
  4. Create an object of ‘xmlwriter’ and use the member functions to add nodes, elements etc.

Linux users

Copy the files xmlwriter.cpp and xmlwriter.h into your working directory. Create a main application, say for example, testxmlwriter.cpp. Create a make file.

#make file created by boby
#March-2006

CC = g++ -Wno-deprecated
CFLAGS = -c

XMLwriter_cygwin : xmlwriter.o testxmlwriter.o
        $(CC) xmlwriter.o testxmlwriter.o  -o XMLwriter_cygwin

xmlwriter.o : xmlwriter.cpp
        $(CC) $(CFLAGS) xmlwriter.cpp

testxmlwriter.o : testxmlwriter.cpp
        $(CC) $(CFLAGS)  testxmlwriter.cpp

clean:
        -rm -f *.o

Section 2: Example implementation

xmlwriter MyXml("boby.xml");
//creates an object of type xmlwriter'. This object 
//is closely coupled to the xml file boby.xml.
//Whatever operation you perform on this object 
//will be reflected in the boby.xml file.
//Constructor adds the following lines to the xml files. 
//Change the constructor code if you want to
//change the encoding format.

<?xml version="1.0" encoding="UTF-8" ?>
MyXml.AddAtributes("age","25");
//add an attribute "age" with value "25" into memory.
//This attribute will be added to the next tag which will be created.
//You can add any number of attributes before adding the tag.
//All the attributes will be added to the next tag created.

MyXml.AddAtributes("Profession","Software");
// add one more attribute.

MyXml.Createtag("Boby");
// this will create a tag boby with two attributes
// "age" and "Profession".

MyXml.AddComment("Personal information");
MyXml.Createtag("Home"); // create node boby

MyXml.CreateChild("Address","Pazheparampil"); // add element

MyXml.CreateChild("Mobile","09844400873");
MyXml.CloseLasttag(); // close boby

MyXml.AddComment("Office information");
MyXml.Createtag("Office"); // create node boby

MyXml.CreateChild("Address","Bangalore"); // add element

MyXml.CreateChild("Ph","0091234567");
MyXml.CloseLasttag(); // close Office


MyXml.CloseAlltags();
//This will colse all the open tags. After going deep 
//into the file creating lots of xml nodes,
//you can close the all opened tags with this single function call.
//A stack has been implemented to keep track of all the open tags.

Section 3: Design overview

Class diagram

Sample screenshot

Summary

This XML writer class is a typical example for a reusable class. Since the class handles only XML operations, this can be easily reused in any application without any code modification.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMember 766473619-Apr-11 20:15 
Good explained and easy to understand...
GeneralThanksmemberhnkulkarni2-Feb-10 16:58 
Thanks for the excellent demo.
GeneralExcellent...saves a lot of workmemberrittjc14-May-09 4:18 
C++ needs a solution similar to XmlWriter on .NET. This works good. Great job!
GeneralSome question for your codememberJacky_y9-Mar-08 23:52 
Hi,Thomas, seems you are the original author of this code, whether you distribute it under any explicit licenses?Look forward to your kindly reply,thanks. Smile | :)
GeneralRe: Some question for your codememberBoby Thomas P31-Mar-08 17:01 
Thanks for your interest. Please adhere to the license specified now. (I didn't read completely. I will read when I get time. Big Grin | :-D )
 
Regards,
Boby

Questionuse feemembersarah_20085-Mar-08 20:48 
Hi, Thomas . I’m interested in your code, I want to confirm could I use it for commercial purpose with no payment to you?
 
Regards,
Sarah.
GeneralRe: use feememberBoby Thomas P31-Mar-08 16:59 
Yes ..you can. Smile | :)
 
Regards,
Boby

Generallicense issuemembernicolas_hotmail4-Mar-08 18:14 
hello, thomas, for your source codes, I want to know which kind of license you are using, I'm not clear whether I have some things to do after I use your codes, thanks
GeneralRe: license issuememberBoby Thomas P4-Mar-08 22:06 
Use/Modify the code at your own risk -Smile | :)
 
Regards,
Boby

GeneralRe: license issuemembernicolas_hotmail5-Mar-08 14:43 
thank you Big Grin | :-D
GeneralDOM and SAXmemberTina Huang20-Aug-07 20:25 
Can we use DOM and SAX to write XML file? Using C++ and STL is straight-forward to generate XML file. However, sometimes if we need to write and read/parse XML file, define the XML schema, and retrieve the attribute and value quickly, is this way still good enough? Thanks.
GeneralRe: DOM and SAXmemberBoby Thomas P20-Aug-07 20:37 
XMLwriter is not DOM/SAX based and is without a reader. It just opens a file and passes information to a file formated in XML. You can't use this class if you want to do read and write.
 
Regards,
Boby

GeneralA minor commentmemberkrivich31-Mar-06 7:12 
Hi!
 
It looks nice. Just some comments/suggestions.
 
1. Spelling:
class xmlwriter -> XmlWriter
Method Closetag -> CloseTag
method CloseLasttag -> CloseLastTag
method AddAttributes -> AddAttribute
method CloseAlltags -> CloseAllTags
 
2. Why attributes are added before the corresponding element was created?
Would it be more logical to call 'AddAttribute' after 'CtateTag' ?
 
Best Wishes,
George.
GeneralRe: A minor commentmemberBoby Thomas P7-May-06 1:07 
Hello,
Thanks for the comments. i will keep tat in mind when i code next time -Smile | :)
 
Regards,
Boby

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 20 Mar 2006
Article Copyright 2006 by Boby Thomas P
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid