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
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
The methods are:
BOOL ParseXML(LPCTSTR XPathString, LPCTSTR strXML);
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
2.type the following on command prompt
c:\XML_Parser_Demo.ocx and press enter. This will register the control on
Inserting Control in your project
the control using regsvr32 command giving XML_Parser_Demo.ocx path as parameter.
( e.g. regsvr32 c:\XML_Parser_Demo.ocx )
a MFC application (selecting "Dialog Based" as application type) using class
to "Project>Add To Project" menu and click on "Components and Controls…" sub
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.
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
a member variable with XML_Parser_Demo Control's (say m_XMLCtrl) using class
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.
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
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
e.g. to parse the following XML string
CString strXML = "
<catalog name="My Catalog" id="146">
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications
<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
- 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.