Click here to Skip to main content
13,798,454 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 21 Sep 2010
Licenced CPOL

Simple C++ XML Parser

, 1 Oct 2010
Rate this:
Please Sign up or sign in to vote.
A Simple C++ XML parser with only the basic functionality


I wrote this article because I was in need of a basic XML parser and could not find one suitable for my needs on the internet (a light weight parser).

The complexity of the parsers out there is rather disarming, and requires a huge amount of knowledge to understand. If you are not a seasoned C++ programmer, it is very hard to make sense of the code, and if you are then you have already written your own parser.

I wrote the parse function, then a set of classes for storing the data parsed, and an example on how to use it (MFC dialog based with a tree view). This parser is very simple and has only the basic functionality in order to work, no fancy stuff. There are some limitations to it:

  • The parser recognizes comments to some extent. An error will be generated if any are found in the XML file outside the root node.
  • As of 29-September-2010, some CDATA support has been added. It is still limited (for example:
    will generate an error). Also, CDATA sections may exist only as node values. Extra implementation can be easily added if needed but given the purpose of this project, I would prefer to leave like this, in order not to complicate the parse function to the extent that it gets difficult to modify.
  • No support for processing instructions
  • No support for DTDs and entities

What is New

This project has been designed with simplicity in mind, in order to be able to assimilate the code base quickly and easily and add to it the extra functionality needed by each specific application.

These classes provide only the basic functionality, therefore is the most lightweight parser from all that I could find: 500 lines of code for the parser (including some unused base64 functions, which can be removed if necessary), and another 500 for the binary tree construct. If more functionality is needed, this has to be added in order to fulfill one's needs.

It is very easy to understand and work with this code as a base for further development.

The parsing function is iterative, it only goes once through the XML string, so the performance is quite satisfactory. The memory requirements are low, each object allocates just how much memory it actually needs. There is much more room for improvement, but it is not the target of this project.

How It Works

I personally think that nothing else needs to be said about this, because the code speaks for itself, and it has been designed to be easy to read and understand. If it is considered necessary, I can go in some details on how it works, and how to be enriched.

Using the Code

Dealing with the XML format, there are three classes (marked with a red dot) :

Cxml, CAttribute and CNode.


Cxml class is the working horse for this project and contains the parse function:

bool Cxml::ParseString(_TCHAR* szXML); 

Once parsed, the information needs to be stored in memory in a manner easy to use, therefore the existence of the Node and Attribute classes.

The Node class has a tree like structure, having a parent pointer and a children list. It also contains a list of Attributes.

An addition to these classes, there is a Utils set of files (.h & .cpp) in which there are some utility functions.

How to Use It?

Well, in order to use it, you have to do the following:

  • Add
    #include "Cxml.h"

    to your project.

  • Create a instance of the class:
    Cxml *oxml = new Cxml();
  • Pass a pointer to the string containing the XML code to the parse function:

After the ParseString returns, the structure of the XML is replicated in the class, and the XML root node can be retrieved with the...


There is a peculiarity here. Because I have adopted the "last in first out" way, the nodes will be organized in a reverse order than they are to be found in the original XML string.

The Node object can be navigated by using its public functions. Remember though that the GetNextChild() function increments the position of the counter and I have not implemented a way to reset it.

The best way to understand the inner workings is to get the demo project and test for yourselves. You will need Visual Studio 2008 to compile the project as is, without reconstruction. If you reconstruct remember: it has not been tested for Unicode.

Download the project and extract it. Compile. Run it and press the load button.


Choose one of the XML files provided as examples. Click open.


This is the result for one of the XML files provided.

        <COMMON>Cardinal Flower</COMMON>
        <BOTANICAL>Lobelia cardinalis</BOTANICAL>


  • September 20, 2010
    • Added
  • September 21, 2010
    • Revised
  • September 22, 2010
    • New version of the project has been updated
      • Corrected a bug for multiple attributes
      • Added 'some' support for comments. Comments outside the root node will generate an error
  • September 28, 2010
    • Tested for unicode, and added a unicode XML for ones test
    • Removed the intellisense file from the project
    • Removed the bug of confusing single quotes and double quotes with the actual delimiter found by Lorenzo Gatti
  • September 29, 2010
    • Added run-time support for UNICODE character set! The program must be compiled using this character set:

    unicode.JPG </li />

  • September 29, 2010
    • Some support for the CDATA sections has been added
    • I also discovered that the Parse function needs some re-factoring because it got heavy and hard to understand.
  • October 1, 2010
    • Replaced all fixed size char arrays from the project with dynamically allocated ones.
    • Added support for processing instructions. All processing instructions are now treated as a special type of node, just like the comments.
    • Also comments can now exist outside the root-node of the XML since now the lowest level node is the XML_DOC node, not the root-node of the XML.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionProblem with Unicode Pin
spyhunter8831-Jul-11 16:36
memberspyhunter8831-Jul-11 16:36 
AnswerRe: Problem with Unicode Pin
BratilaRazvan31-Jul-11 23:09
memberBratilaRazvan31-Jul-11 23:09 
Rant[My vote of 2] Better than 1 or:my vote of 2 Pin
cccfff77726-Apr-11 6:02
membercccfff77726-Apr-11 6:02 
GeneralMy vote of 5 Pin
maplewang21-Oct-10 15:57
membermaplewang21-Oct-10 15:57 
GeneralMy vote of 1 [modified] Pin
Aescleal4-Oct-10 12:05
memberAescleal4-Oct-10 12:05 
General[My vote of 1] Sorry mate but waste of time Pin
Galatei3-Oct-10 1:58
memberGalatei3-Oct-10 1:58 
GeneralRe: [My vote of 1] Sorry mate but waste of time [modified] Pin
Lorenzo Gatti3-Oct-10 22:36
memberLorenzo Gatti3-Oct-10 22:36 
GeneralRe: [My vote of 1] Sorry mate but waste of time Pin
BratilaRazvan5-Oct-10 0:46
memberBratilaRazvan5-Oct-10 0:46 
GeneralRe: [My vote of 1] Sorry mate but waste of time Pin
Aescleal5-Oct-10 2:19
memberAescleal5-Oct-10 2:19 
GeneralRe: [My vote of 1] Sorry mate but waste of time Pin
BratilaRazvan5-Oct-10 2:38
memberBratilaRazvan5-Oct-10 2:38 
GeneralRe: [My vote of 1] Sorry mate but waste of time Pin
Aescleal5-Oct-10 4:24
memberAescleal5-Oct-10 4:24 
GeneralMy vote of 1 [modified] Pin
Lorenzo Gatti27-Sep-10 22:11
memberLorenzo Gatti27-Sep-10 22:11 
GeneralRe: My vote of 1 Pin
BratilaRazvan28-Sep-10 3:21
memberBratilaRazvan28-Sep-10 3:21 
GeneralRe: My vote of 1 Pin
BratilaRazvan28-Sep-10 5:12
memberBratilaRazvan28-Sep-10 5:12 
GeneralRe: My vote of 1 Pin
BratilaRazvan29-Sep-10 0:22
memberBratilaRazvan29-Sep-10 0:22 
GeneralRe: My vote of 1 Pin
BratilaRazvan1-Oct-10 4:29
memberBratilaRazvan1-Oct-10 4:29 
GeneralRe: My vote of 1 Pin
Lorenzo Gatti3-Oct-10 22:55
memberLorenzo Gatti3-Oct-10 22:55 
GeneralI don't agree with the "My Vote of..." PinPopular
FrankLaPiana27-Sep-10 16:51
memberFrankLaPiana27-Sep-10 16:51 
GeneralMy vote of 1 Pin
xComaWhitex24-Sep-10 21:50
memberxComaWhitex24-Sep-10 21:50 
GeneralRe: My vote of 1 Pin
BratilaRazvan24-Sep-10 23:24
memberBratilaRazvan24-Sep-10 23:24 
GeneralRe: My vote of 1 Pin
xComaWhitex24-Sep-10 23:38
memberxComaWhitex24-Sep-10 23:38 
GeneralRe: My vote of 1 Pin
BratilaRazvan25-Sep-10 0:18
memberBratilaRazvan25-Sep-10 0:18 
GeneralRe: My vote of 1 Pin
xComaWhitex25-Sep-10 0:26
memberxComaWhitex25-Sep-10 0:26 
GeneralRe: My vote of 1 Pin
BratilaRazvan25-Sep-10 0:36
memberBratilaRazvan25-Sep-10 0:36 
GeneralRe: My vote of 1 Pin
xComaWhitex25-Sep-10 0:38
memberxComaWhitex25-Sep-10 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181207.3 | Last Updated 1 Oct 2010
Article Copyright 2010 by BratilaRazvan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid