Introduction

Following is a quick and dirty C++ XML XPath expression parser.

Background

I started the day checking the web looking for a quick and easy XPath parser for C++. Now, before I begin, I just want to say that I found plenty of XML libraries out there but nothing that quite suited my needs. What got me started on this XPath adventure, you ask? Well, it all started because I am currently writing a dev tool which requires some external settings. Usually, I would place these app settings in the Registry, but on this occasion (as I had a little time), I thought I would have a go at using XML to store my application settings. All was going swimmingly, and I had got to the part where I had written my settings.xml file, and now I wanted to read it back in and navigate my settings file. After doing some internet prodding, it seems that XPath is the easiest and simplest way to do this.

I had a quick play with the .NET classes using C++/CLI and found them a breeze to use, but I wanted to keep my application to be fully C++ native on this occasion, which is why I decided to write this quick and dirty XPathParser class.

How to Use the XPathParser Class

Using the XPathParser class is very straightforward.

First, add the source files (XPathParser.cpp and XPathParser.h) into the directory you wish to use them from, and then add the files to your Visual Studio project. Include XPathParser.h in the file you want to use the class:

#include"XPathParser.h"

The next thing to do is to include the namespace XPathNS.

usingnamespace XPathNS;

At this point, we are ready to rock and roll. All we need to do is instantiate an instance of the XPathParser, passing through the name of the XML file we are going to throw XPath expressions at.

XPthParser xPath( "books.xml" )

If you have made it this far, you'd be foolish to stop coding now. Now, we get to try out some XPath expressions (just like below):

Simple as hey! Are we forgetting something? Ahh yes.. Each XPath expression yields a list of XMLNodes. An XMLNode is made up of the "XML name", the actual "XML value", and "any XML attributes" associated with the XMLNode. For reference, here is what the XMLNode class looks like:

Your XPath Parser is not lightweight but I believe it is dirty since you are just wrapping the XPath functionality found in MSXML6. MSXML6 is certainly not lightweight. IMHO, your code and article is a sham.

"We make a living by what we get, we make a life by what we give." --Winston Churchill

Hello George. Thankyou for your comments. Yes you are correct i am just wrapping MSXML6, which is why i titled article as "Quick & Dirty". I did not intend to convey that this code was lightweight. Its just a quick and easy way to get to use XPath.

Following your comments I have tweaked my article and removed the one occurance that suggested this code was lightweight.