A Template-Based Approach to XML Parsing in C++
The next aspect of our XML-parsing framework is converting XML tags into domain-related objects that can be used within the application by using templates and a loose definition of policy classes.
The XMLDomainMap template accepts a single template parameter, called an XMLNode. The interface for the domain-mapping object is as follows:
XMLDomainMap
createaddupdateAttribute
The XMLNode acts as both a leaf and a root in a tree structure that aggregates its children as a subtree. The XMLNode's interface is:
XMLNode
operator==operator!=operator=addChildhasChildrennumChildrenvaluenamegetChildCountgetChildgetParent
The key here is the design of the public interface of the object. There are several operator overloads, specifically operator equals (operator==), operator not equals (operator!=) and the assignment operator (operator=). The benefit to this is the object now can be used with many standard template library (STL) containers and algorithms, which allows for the use of advanced features with the C++ language.
Thus far, the focus has been on individual classes and describing the templates that have been created for our XML-processing framework. The next step is to link the disparate interfaces together and make them appear to function as a single cohesive unit by using the façade design pattern.
The façade design provides a simple and elegant way to delegate parsing functionality from an outside client to the internal policy class that will be used for performing the parsing.
In Design Patterns, the authors define the intent as to “Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.”
The XMLProcessor is the façade that has been created. It is defined with the following interface:
XMLProcessor
parsegetParseEngine
Once all the source has been written, an XML file and a test client will be needed to run our sample.
The following simple XML file, showing the basic layout of a customer record with a name and account number, has been created to illustrate the simplicity of using the framework:
<?xml version="1.0"
encoding="iso-8859-1"?>
<customer>
<name>John Doe</name>
<account-number>555123</account-number>
</customer>
For now, create this file with a text editor and save it as MyXMLFile.xml.
The framework's functionality will be used as a mechanism to provide a succinct interface to the client application.
The primary methods that a client of the framework would make use of can be described with an actual, albeit small, sample of C++ source code:
// ---------------------------------------
// Sample source for parsing an XML doc
// ---------------------------------------
#include "XMLProcessor.hpp"
#include "XMLDomainMap.hpp"
#include "XMLSAXParser.hpp"
#include "XMLNode.hpp"
#include "XMLCommand.h"
#include "XMLSAXHandler.hpp"
#include "XMLSAXErrorHandler.hpp"
#include <iostream>
using namespace std;
using namespace XML;
// Let's get the ugly stuff out of the way first
typedef XMLSAXHandler<XMLDomainMap<XMLNode> >
DOCHANDLER;
typedef XMLSAXErrorHandler ERRORHANDLER;
typedef XMLSAXParser<DOCHANDLER, ERRORHANDLER>
PARSER;
typedef XMLProcessor<PARSER> XMLEngine;
// Create a basic test client
int main(void)
{
// Define a string object with our filename
std::string xmlFile = "MyXMLFile.xml";
// Create an instance of our XMLFactory
XMLEngine parser(xmlFile);
// Turn off validation
parser.doValidation(false);
// Parse our XML file
parser.parse();
// Get the tree root
XMLNode root = parser.getXMLRoot();
// Print the name of our object
cout << "Root = " << root.name() <<
endl;
return 0;
}
Now that an instance of an XMLNode object representing the root of the tree has been parsed, the child elements of the root XMLNode can be accessed.
The last step is to compile the client. Simply perform the compile at the command line:
g++ -o testClient -I. -I/path/to/xerces/include \ -I/path/to/xerces/include/xerces testClient.cpp \ -L/path/to/xerces/lib -lxerces-c
This compiles the client application. The next step is to run a test. Be sure to set your LD_LIBRARY_PATH environment variable to point to the location of your Xerces installation's lib directory. Because the shared libraries are from this directory, the application loader needs a way to import the required symbols at runtime in order for everything to function correctly.
When testClient is run, the following output is expected:
$>testClient Adding child name Adding child account-number Root = customer
You now have a fully functional XML-parsing framework using C++ templates that will allow you to incorporate XML into your new or existing applications. Sample code is available at ftp.linuxjournal.com/pub/lj/listings/issue110/6655l1.tgz.

John Dubchak is a senior software developer working as a consultant in the St. Louis area. He's been programming in C++ for the past 12 years and can't believe how bad his first lines of C++ actually were. His wife says that his hobby is “sitting at the computer writing little programs”.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
1 hour 18 min ago - Reply to comment | Linux Journal
1 hour 34 min ago - Favorite (and easily brute-forced) pw's
3 hours 25 min ago - Have you tried Boxen? It's a
9 hours 17 min ago - seo services in india
13 hours 49 min ago - For KDE install kio-mtp
13 hours 49 min ago - Evernote is much more...
15 hours 49 min ago - Reply to comment | Linux Journal
1 day 35 min ago - Dynamic DNS
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Sample Code
Hey this code doesn't work. Missing XMLCommand.h. Please provide the same.
Regards
code with out using in built functions
xml parsing program
without using in built functions
Hi, I can't make the code
Hi,
I can't make the code compile..
One file is missing: XMLCommand.h.
can you supply it?
thanks,
XMLCommand.h
Hi John,
Can you please share the XMLCommand.h?
-Gunjan
sample code
Hi,
sounds interesting, but I can't make the code compile..
One file is missing: XMLCommand.h.
can you supply it?
thanks,
pierre