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






4.72/5 (43 votes)
Mar 20, 2006
2 min read

80925

2595
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: bobypt@gmail.com 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.
- 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.
- No platform dependency. Easily move to any platform.
Features missing/Limitations
- 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.
- Provision for processing instructions is not implemented.
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: Section 2: Example Please see the link to source files for more details.<SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"> rm -f *.o
//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();
Section 3: Design overview.
Class Diagram
Object Diagram
Summary
For simple xml manipulation, you can use this class. This is a typical example for reusable class.