Introduction
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.
Operation
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).
Features
This control is invisible at runtime, and exposes two methods and three
events.
The methods are:
BOOL LoadXMLFromFile();
BOOL ParseXML(LPCTSTR XPathString, LPCTSTR strXML);
and the events are:
void FoundRootElement(BSTR ElementName, BSTR AttributesList);
void FoundChildNode(BSTR strNodeName, BSTR strNodeAttributes, BSTR
strParentNodeName);
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 regsvr32
c:\XML_Parser_Demo.ocx
and press enter. This will register the control on
your system
Steps
Inserting Control in your project
- Register
the control using regsvr32 command giving XML_Parser_Demo.ocx path as parameter.
( e.g. regsvr32 c:\XML_Parser_Demo.ocx )
- Create
a MFC application (selecting "Dialog Based" as application type) using class
wizard.
- Go
to "Project>Add To Project" menu and click on "Components and Controls�" sub
menu.
- 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.
- 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
- Attach
a member variable with XML_Parser_Demo Control's (say m_XMLCtrl) using class
wizard.
- 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.
- 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.
- 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>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>";
call
m_XMLCtrl.ParseXML("//book",strXML);
or to parse only first <book> element call
m_XMLCtrl.ParseXML("//book[1]",strXML);
or to parse whole XML string call
m_XMLCtrl.ParseXML("//catalog",strXML);
- 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.