Click here to Skip to main content
13,189,802 members (62,455 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 7 Oct 2004

A DOM based XML Parser that Fires Events as SAX based XML Parsers do

, 10 Oct 2004
Rate this:
Please Sign up or sign in to vote.
A simple ActiveX control which parses XML documents and fires events when it encounters Elements (Root, Nodes, Leaves, Body) in an XML Document.

Sample Image - XML_Parser_Ctrl.gif


For a newbie, parsing an XML document has always been a challenge, specially if you don’t have concepts of traversing a tree. This parser encapsulates MSXML and is based upon DOM way of parsing XML (i.e., maintaining a tree from XML document).

The distinguishing feature about my parser is that although it is DOM based, once the tree is maintained, it traverses the tree and fires events in the same way as a SAX based parser does when it encounters elements.


The operation is very simple. I first load the entire XML in IXMLDOMDocument object, and then traverse the tree starting from root node using Depth-First algorithm (thanks to my teacher Sir Shahab for teaching me Data Structures in such an efficacious manner).


This control is invisible at runtime, and exposes two methods and three events.

The methods are:

  • BOOL LoadXMLFromFile();

and the events are:

  • void FoundRootElement(BSTR ElementName, BSTR AttributesList);
  • void FoundChildNode(BSTR strNodeName, BSTR strNodeAttributes, BSTR 
  • void FoundLeaf(BSTR strLeafName, BSTR strLeafAttrib, BSTR 
    strBodyValue, BSTR strParentNode);

If a node has attributes, it will be passed as “;” separated name=value pairs to container application.

Before Using the control

before using the control donot forget to register it first on your system. To do this follow these steps.

1. Download XML Parser Control - 17.4 Kb and extract it on any folder say c:\XML_Parser_Ctrl_src

2.type the following on command prompt

and press enter. This will register the control on your system


Inserting Control in your project

  1. Register the control using regsvr32 command giving XML_Parser_Demo.ocx path as parameter. ( e.g. regsvr32 c:\XML_Parser_Demo.ocx )
  2. Create a MFC application (selecting "Dialog Based" as application type) using class wizard.
  3. Go to "Project>Add To Project" menu and click on "Components and Controls…" sub menu.
  4. In the "Components and Controls Gallery" dialog box, that appears, double click on "Registered ActiveX Controls> XML_Parser_Demo Control". Click O.K to all the messages that follow and then close the dialog box.
  5. Open your dialog box in resource view (if it is not already opened), you can see XML_Parser_Demo Control's icon (saying "XML") with in the Controls rebar. Click on this icon, drag it to your dialog box and then drop it over there.

Using the Control

  1. Attach a member variable with XML_Parser_Demo Control's (say m_XMLCtrl) using class wizard.
  2. press Ctrl+W and in the class wizard dialog box that appears, on the "Message Map" tab, select IDCXMLPARSERDEMOCTRL1 from "Object Ids" list and then add Event Handlers for FoundRootElement, FoundChildNode and FoundLeaf events by double clicking each one and accepting default names.
  3. if you want to parse the XML document saved on disk then call m_XMLCtrl.LoadXMLFromFile(); this will load your XML document,will start parsing it from root node and will fire events such as FoundRootElement, FoundChildNode and FoundLeaf as it encounters Root, Nodes and Leafs respectively calling OnFoundRootElementXmlparserdemoctrl1, OnFoundChildNodeXmlparserdemoctrl1, OnFoundLeafXmlparserdemoctrl1.
  4. Or if you want to parse XML string then call m_XMLCtrl.ParseXML(…) passing Node from where to start parsing name preceded with "//" and XML string as arguments.

e.g. to parse the following XML string

CString strXML = "
<catalog name="My Catalog" id="146">
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications 
with XML.</description>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<description>A former architect battles corporate zombies, 
an evil sorceress, and her own childhood to become queen 
of the world.</description>



or to parse only first <book> element call


or to parse whole XML string call

  1. compile and run the program.

Finally, I would like to dedicate this project to a very special and supreme friend of mine who once asked me for such type of an XML parser.


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

Muhammad Azam
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralCan't register ocx on Win7 Pin
strNeedHelp25-May-10 18:34
memberstrNeedHelp25-May-10 18:34 
Generalerror on registration Pin
areil15-Dec-05 22:30
memberareil15-Dec-05 22:30 
GeneralRe: error on registration Pin
nikomsj29-Mar-06 0:12
membernikomsj29-Mar-06 0:12 
QuestionPurpose? Pin
Erik Rydgren13-Oct-04 23:03
memberErik Rydgren13-Oct-04 23:03 
AnswerRe: Purpose? Pin
Muhammad Azam14-Oct-04 0:06
memberMuhammad Azam14-Oct-04 0:06 

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
Web03 | 2.8.171016.2 | Last Updated 11 Oct 2004
Article Copyright 2004 by Muhammad Azam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid