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 XMLWriter
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 own risk. (Please test the application.)
Features
- Easily creates nodes
- Easily creates elements
- Easily adds attributes
- Closes all the open tags with one simple command (Stack to keep track of all the open tags)
- No need to worry about file operations (All handled internally)
Features Missing
- No verification and validation is done on the file (User's responsibility to ensure that file is well formed and valid)
- 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:
Usage is very simple and easy.
- Create a simple project in the IDE that you are using.
- Add the files xmlwriter.cpp and xmlwriter.h.
- Create a main application.
- Create an object of ‘
xmlwriter
’ and use the member functions to add nodes, elements, etc.
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
xmlwriter MyXml("boby.xml");
<?xml version="1.0" encoding="UTF-8" ?>
MyXml.AddAtributes("age","25");
MyXml.AddAtributes("Profession","Software");
MyXml.Createtag("Boby");
MyXml.AddComment("Personal information");
MyXml.Createtag("Home");
MyXml.CreateChild("Address","Pazheparampil");
MyXml.CreateChild("Mobile","09844400873");
MyXml.CloseLasttag();
MyXml.AddComment("Office information");
MyXml.Createtag("Office");
MyXml.CreateChild("Address","Bangalore");
MyXml.CreateChild("Ph","0091234567");
MyXml.CloseLasttag();
MyXml.CloseAlltags();
Class Diagram
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.