Click here to Skip to main content
15,868,340 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
4.72/5 (46 votes)
20 Mar 20062 min read 79.6K   2.6K   46   9
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="mailto:bobypt@gmail.com">bobypt@gmail.com</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.

 

Introduction

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

Makefile
 
<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
#March-2006
 
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

clean:

<SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana">               rm  -f *.o

Section 2: Example

<u1:p>
 //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();
                 cout<<sEname.c_str();
 
// 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.
         XMLDoc.ResetReading(); 
 
// Explicit call to delete the dom structure. (Called again in desstructor);
         XMLDoc.DeleteAll();

   

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

 <u1:p> <o:p>

Section 3: Design overview. <o:p>

<u1:p>

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

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

Summary

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

License

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


Written By
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions

 
GeneralThank you very much Pin
caesten19-May-09 12:01
caesten19-May-09 12:01 
GeneralRead Attribute Pin
iyadounove19-Apr-08 2:19
iyadounove19-Apr-08 2:19 
GeneralRe: Read Attribute Pin
haily_lee10-Dec-10 6:44
haily_lee10-Dec-10 6:44 
GeneralCompilation Warning Pin
Stefano_Malavasi6-Jun-06 23:21
Stefano_Malavasi6-Jun-06 23:21 
GeneralRe: Compilation Warning Pin
Antonymous25-Sep-06 9:35
Antonymous25-Sep-06 9:35 
GeneralCompilation Failed Pin
Fernando A. Gomez F.7-Apr-06 12:20
Fernando A. Gomez F.7-Apr-06 12:20 
GeneralRe: Compilation Failed Pin
Fernando A. Gomez F.7-Apr-06 12:25
Fernando A. Gomez F.7-Apr-06 12:25 
I added the following:

<br />
using namespace std;<br />
	<br />
_Lockit::_Lockit()<br />
{<br />
}<br />
<br />
_Lockit::~_Lockit() <br />
{ <br />
}<br />


at the end of XMLReader.cpp, and compiles and links good. However, I don't know whether this will work or not... Sigh | :sigh: Cry | :((

Regards,
Fernando

A polar bear is a bear whose coordinates has been changed in terms of sine and cosine.
GeneralRe: Compilation Failed Pin
Boby Thomas P7-May-06 1:09
Boby Thomas P7-May-06 1:09 
QuestionTinyXml? Pin
Gilad Novik27-Mar-06 4:29
Gilad Novik27-Mar-06 4:29 

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.