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

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

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.)

Why use XMLreader.

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

Features missing/Limitations

  1. Implementation uses Document Object Model. So do not use this for reading big xml files.
  2. Provision for processing instructions is not implemented.
  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).
  4. 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.)

Section 1: How to use?

            Usage is very simple and easy.

Windows users

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.

 Linux/Cygwin 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 as what is given below.

 

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

 //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.

 

Section 3: Design overview.

Class Diagram

Sample screenshot

Object Diagram

  Sample screenshot

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

Share

About the Author

Boby Thomas P
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions

 
GeneralThank you very much Pinmembercaesten19-May-09 12:01 
GeneralRead Attribute Pinmemberiyadounove19-Apr-08 2:19 
GeneralRe: Read Attribute Pinmemberhaily_lee10-Dec-10 6:44 
GeneralCompilation Warning PinmemberStefano_Malavasi6-Jun-06 23:21 
Hi,
I have compiled xmlreader.cpp / xmlreader.h under Visual C++ 6.0 (Windows XP SP2) but I got those warnings. How can I fix this ?
 
c:\programmi\microsoft visual studio\vc98\include\xtree(118) : warning C4786: 'std::_Tree,std::allocator >,std::pair,std::allocator > const ,std:
:basic_string,std::allocator > >,std::map,std::allocator >,std::basic_string,std::allocator >,std::less,std::allocator > >,std::allocator,std::allocator > > >::_Kfn,std::less,std::allocator > >,std::allocator,std::allocator > > >' : identifier was truncated to '255' characters in the debug information
c:\programmi\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree,std::allocator >,std::pair,std::allocator > const ,std::basic_string,std::allocator > >,std::map,std::allocator >,std::basic_string,std::allocator >
,std::less,std::allocator > >,std::allocator,std::allocator > > >::_Kfn,std::less,std::allocator > >,std::allocator,std::allocator > > >' being compiled
c:\sorgenti\xmlimport\xmlreader.h(41) : see reference to class template instantiation 'std::map,std::allocator >,std::basic_string,std::allocator >,std::le
ss,std::allocator > >,std::allocator,std::allocator > > >' being compiled

GeneralRe: Compilation Warning PinmemberAntonymous25-Sep-06 9:35 
GeneralCompilation Failed PinmemberFernando A. Gómez F.7-Apr-06 12:20 
GeneralRe: Compilation Failed PinmemberFernando A. Gómez F.7-Apr-06 12:25 
GeneralRe: Compilation Failed PinmemberBoby Thomas P7-May-06 1:09 
QuestionTinyXml? PinmemberGilad Novik27-Mar-06 4:29 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 20 Mar 2006
Article Copyright 2006 by Boby Thomas P
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid