Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
10 Oct 2004 1  
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

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

  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>
<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);
  1. compile and run the program.

Sample screenshot

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.

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