Title: XMLreader - Simple reusable class (DOM implementation) for reading xml data.
Author: Boby Thomas
Email: <A href="mailto:email@example.com">firstname.lastname@example.org</A>
Environment: VC++ 6.0, Win95/98,2000, Linux
Keywords: XML reader, DOM
Description: XMLreader - Simple reusable class (DOM implementation) for reading xml data.
Section C++, DOM, XML.
XMLreader is a simple class that can be used in your application to read an xml file. All you need to do is include the xmlreader.h and xmlreader.cpp into your project and enjoy.
The implementation uses only standard cpp libraries and STLs, and so this can be used for applications in windows as well as Linux. I have used this in 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 xmlreader class doesn’t contain all the features of standard 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 your application.)
- Very simple to use.
- No platform dependency. Easily move to any platform.
- Easy traversal through the xml document.
- Very easy to extract element values.
- Uses standard Document object Model (DOM). But complete memory allocation and memory deletion is hidden inside the class.
- Implementation uses Document Object Model. So do not use this for reading big xml files.
- Provision for processing instructions is not implemented.
- Provision to retrieve elements attributes is not implemented (to keep the example simple). But this can be easily added since the whole code is in front of you. But xml attribute values will be ignored (The xmlreader class can be used without any modifications if you are not interested in xml element attributes).
- Lot more. You can add on.
I have organized the tutorial in the following way.
Section 1: How to use?
Section 2: Example implementation.
Section 3: Design overview. (So that you can add, remove and modify existing features.)
Usage is very simple and easy.
1 Create a simple project in the IDE what 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 obtain any value from xml file.
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 as what is given below.
<SPAN style="FONT-SIZE: 6.5pt"><SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-SIZE: 14pt">#make file created by boby
CC = g++
CFLAGS = -c
XMLreader_cygwin : XMLreader.o XMLreaderTest.o
$(CC) XMLreader.o XMLreaderTest.o -o XMLreader_cygwin
XMLreader.o : XMLreader.cpp
$(CC) $(CFLAGS) XMLreader.cpp
XMLreaderTest.o : XMLreaderTest.cpp
$(CC) $(CFLAGS) XMLreaderTest.cpp
<SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"> rm -f *.o
Section 2: Example
int iObjCnt = XMLNode::GetObjectCnt();
cout<<"\nNumber of Node objects "<<iObjCnt<<"\n";
XMLNode * NodePtr = XMLDoc.GetRootNode();
XMLNode *NodePtr2 = NodePtr->GetFirstChild();
while(NodePtr2 != NULL)
string sEname = NodePtr2->GetNodeName();
string sVal = NodePtr2->GetElementValue("phone"); cout<<"\tPhone\t:"<<sVal.c_str()<<"\n";
NodePtr2 = NodePtr->GetNextChild();
Please see the link to source files for more details.
For simple xml manipulation, you can use this class. This is a typical example for reusable class.