Click here to Skip to main content
13,451,455 members (46,904 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 20 Mar 2006

XMLreader - Simple reusable class (DOM implementation) for reading xml data.

, 20 Mar 2006
Rate this:
Please Sign up or sign in to vote.
XMLreader - Simple reusable class (DOM implementation) for reading xml data.
Title:       XMLreader - Simple reusable class (DOM implementation) for reading xml data.
Author:      Boby Thomas
Email:       <A href=""></A>
Environment: VC++ 6.0, Win95/98,2000, Linux
Keywords:    XML reader, DOM
Level:       Intermediate
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. <?XML:NAMESPACE PREFIX = U1 /><u1:p> <?xml:namespace prefix = o /><o:p>

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.<o:p>

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.) <o:p>

Why use XMLreader.<u1:p> <o:p>

  1. Very simple to use.<o:p>
  2. No platform dependency. Easily move to any platform.<o:p>
  3. Easy traversal through the xml document.<o:p>
  4. Very easy to extract element values.<o:p>
  5. Uses standard Document object Model (DOM). But complete memory allocation and memory deletion is hidden inside the class.<o:p>

Features missing/Limitations<u1:p> <o:p>

  1. Implementation uses Document Object Model. So do not use this for reading big xml files.<o:p>
  2. Provision for processing instructions is not implemented.<o:p>
  3. 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).<o:p>
  4. Lot more. You can add on.<o:p>

 <u1:p> <o:p>

I have organized the tutorial in the following way.<o:p>

Section 1: How to use?<o:p>

Section 2: Example implementation.<o:p>

Section 3: Design overview. (So that you can add, remove and modify existing features.)<o:p>

Section 1: How to use?<u1:p>

            Usage is very simple and easy.<o:p>

Windows users<u1:p> <o:p>

1        Create a simple project in the IDE what you are using.<o:p>

2        Add the files xmlwriter.cpp and xmlwriter.h.<o:p>

3        Create a main application.<o:p>

4        Create an object of ‘xmlwriter’ and use the member functions to obtain any value from xml file.<o:p>

 Linux/Cygwin users.<u1:p> <o:p>

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.<o:p>

 <u1:p> <o:p>

<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++
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

 //pass the xml file name to constructr. Each 
// object is closely coupled to an xml file. Constructor reads the xml file 
// Creates the DOM structure in memory and release the xml file
         XMLRdr XMLDoc("boby.xml"); 
// Returns the number of node objects created.
         int iObjCnt = XMLNode::GetObjectCnt(); 
         cout<<"\nNumber of Node objects "<<iObjCnt<<"\n";
// returns a pointer to root node.
         XMLNode * NodePtr = XMLDoc.GetRootNode();
// Returns a pointer to first child node of the node NodePtr.
         XMLNode *NodePtr2  = NodePtr->GetFirstChild();       
         while(NodePtr2 != NULL)
                 string sEname = NodePtr2->GetNodeName();
// Returns element value for the tag phone          
string sVal = NodePtr2->GetElementValue("phone");            cout<<"\tPhone\t:"<<sVal.c_str()<<"\n";
// returns pointer to next child node. This function can be
// Used to traverse through all the child nodes of a particular node.
                 NodePtr2 = NodePtr->GetNextChild();
// reset´the variable used to traverse. Call this function if you are going to
// parse throgh the xml file again and again.
// Explicit call to delete the dom structure. (Called again in desstructor);


Please see the link to source files for more details.<o:p>

 <u1:p> <o:p>

Section 3: Design overview. <o:p>


Class Diagram<u1:p> <o:p>

<?XML:NAMESPACE PREFIX = U2 /><U2:SHAPETYPE id=_x0000_t75 coordsize="21600,21600" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f" u1:spt="75" u1:preferrelative="t"><U2:STROKE joinstyle="miter"><u2:formulas><U2:F eqn="if lineDrawn pixelLineWidth 0"><U2:F eqn="sum @0 1 0"><U2:F eqn="sum 0 0 @1"><U2:F eqn="prod @2 1 2"><U2:F eqn="prod @3 21600 pixelWidth"><U2:F eqn="prod @3 21600 pixelHeight"><U2:F eqn="sum @0 0 1"><U2:F eqn="prod @6 1 2"><U2:F eqn="prod @7 21600 pixelWidth"><U2:F eqn="sum @8 21600 0"><U2:F eqn="prod @7 21600 pixelHeight"><U2:F eqn="sum @10 21600 0"><U2:PATH gradientshapeok="t" u1:extrusionok="f" u1:connecttype="rect"><U1:LOCK aspectratio="t" u2:ext="edit"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 o:spt="75" o:preferrelative="t" coordsize="21600,21600" path=" m@4@5 l@4@11@9@11@9@5 xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0 "><v:f eqn="sum @0 1 0 "><v:f eqn="sum 0 0 @1 "><v:f eqn="prod @2 1 2 "><v:f eqn="prod @3 21600 pixelWidth "><v:f eqn="prod @3 21600 pixelHeight "><v:f eqn="sum @0 0 1 "><v:f eqn="prod @6 1 2 "><v:f eqn="prod @7 21600 pixelWidth "><v:f eqn="sum @8 21600 0 "><v:f eqn="prod @7 21600 pixelHeight "><v:f eqn="sum @10 21600 0 "><v:path o:connecttype="rect" o:extrusionok="f" gradientshapeok="t"><o:lock v:ext="edit" aspectratio="t">Sample screenshot<v:shape id=_x0000_i1025 style="WIDTH: 429pt; HEIGHT: 225pt" type="#_x0000_t75" coordsize="21600,21600"><v:imagedata o:title="codepr9" src="./xmlreader_files/image001.jpg"><o:p>

Object Diagram<u1:p> <o:p>

 <u1:p> Sample screenshot<o:p>


For simple xml manipulation, you can use this class. This is a typical example for reusable class.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Boby Thomas P
Software Developer (Senior) DWS
Australia Australia

You may also be interested in...

Comments and Discussions

GeneralCompilation Failed Pin
Fernando A. Gómez F.7-Apr-06 12:20
memberFernando A. Gómez F.7-Apr-06 12:20 
GeneralRe: Compilation Failed Pin
Fernando A. Gómez F.7-Apr-06 12:25
memberFernando A. Gómez F.7-Apr-06 12:25 
GeneralRe: Compilation Failed Pin
Boby Thomas P7-May-06 1:09
memberBoby Thomas P7-May-06 1:09 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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