Click here to Skip to main content
11,637,415 members (64,764 online)
Click here to Skip to main content

CXMLFile - A Simple C++ XML Parser

, 19 Mar 2008 CPOL 133.5K 8.5K 68
Rate this:
Please Sign up or sign in to vote.
An article on simple and fast C++ XML parser

Introduction

This article is about a simple and fast C++ XML parser class. There is often a need for an effective XML parser that is able to load the XML document, validate it, and browse it. In .NET environment there is a large native support for handling a lot of types of XML documents, but the same native support is missing from the original C++, MFC etc. There is, however, a COM alternative for XML file parsing and handling but it takes some time to learn it, and to use it in the right way.

This article is a simple attempt to make a C++ developer's life a bit easier than it usually is. This is support for handling the well-formed XML documents in the simplest possible way: load it, validate it, and browse it. This supports the following XML elements:

  • A simple TAG element, like <Element>
  • A simple ATTRIBUTE element, like Attribute="Value"
  • A simple TEXT element, like [Text]

Below is an example of a simple XML file that is supported:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <note>
        <to>Tove</to>
        <from>Jani</from>

        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
    </note>

The presented XML classes are able to load this type of XML document, check if it is well-formed, and browse throughout its content. There are only two classes that provide this functionality.

The first class is called the CXMLFile class, and its main purpose is to load an XML file, validate its structure, and create an XML element collection out of its content. This collection of XML elements will represent the loaded XML file in the system memory. Its easy then to modify the inner struture of this collection, that is, to modify the XML file itself. This class also supports the loading of XML files from the hard-disk or from the memory stream, which is a special usage (ie. on some web server). The CXMLFile class can also output the XML element collection from the system memory to the file on the hard-disk.

The second class is called the CXMLElement class. It is used by the previous class, and will be used by the developer when browsing or modifying the inner structure of an XML file in the system memory, that is, when modifying the inner structure of the XML element collection. It has the basic support for the appending of this collection, and browsing it. It can provide information regarding the name, type or value of the current XML element from the collection.

Background

There are many articles on the CodeProject considering this topic, and this is a small contribution to these articles population. Hope that the readers and developers will find it useful in their everyday work.

Using the Code

It's quite easy to load an XML document from the hard-disk. See an example below:

#include <span class="code-string">"XMLFile.h"</span>

...

_TCHAR lpszXMLFilePath[] = _T("A path to the XML file here...");
CXMLFile xmlFile;
if (xmlFile.LoadFromFile(lpszXMLFilePath))
{
   // Success
}
else
{
   // Error
}

To load an XML document from the memory stream:

...

// lpData and dwDataSize are obtained elsewhere

CXMLFile xmlFile;
if (xmlFile.LoadFromStream(lpData, dwDataSize))
{
    // Success
}
else
{
    // Error
}

To save the XML element collection to the file on the hard-disk, do the following:

if (xmlFile.SaveToFile(lpszXMLFilePath))
{
    // Success
}
else
{
    // Error
}

After the call to LoadFromFile(), a method of the CXMLFile class, the validation and parsing of the custom XML file will be done. If the XML file is well-formed, it will be loaded in the system memory as collection of CXMLElement elements. One can gain access to this collection using another method of the CXMLFile class called GetRoot(). See below:

CXMLEElement* pRoot = xmlFile.GetRoot();

Having the pointer to the root-element of the XML collection in the system memory, there are some things that can be done here. The root-element of the collection is of the CXMLEElement class type. Here are the methods available:

// Returns the name of the current XML element
LPTSTR GetElementName();
// Returns the type of the current XML element
XML_ELEMENT_TYPE GetElementType();
// Returns the number of child elements of the current XML element
int GetChildNumber();
// Returns the first child element of the current XML element
CXMLElement* GetFirstChild();
// Returns the current child element of the current XML element
CXMLElement* GetCurrentChild();
// Returns the next child element of the current XML element
CXMLElement* GetNextChild();
// Returns the last child element of the current XML element
CXMLElement* GetLastChild();
// Sets the value of the current XML element (valid only for attribute elements)
void SetValue(LPTSTR lpszValue);
// Gets the value of the current XML element (valid only for attribute elements)
LPTSTR GetValue();

Modify the inner structure of the XML element collection using the following methods:

// Create the new XML element of the specified type
void Create(LPTSTR lpszElementName, XML_ELEMENT_TYPE type);
// Appends the new XML element to the end of the collection of the current XML element
void AppendChild(CXMLElement* lpXMLChild);

Using the first group of CXMLEElement class methods, one can browse the XML element collection. Using the second group of CXMLEElement class methods, one can create new XML elements of different types and append them to existing ones.

Speaking about the types of XML elements, here are they listed:

XET_TAG // TAG element
XET_ATTRIBUTE // ATTRIBUTE element
XET_TEXT // TEXT element

Points of Interest

I always had a problem with loading XML documents easily and manipulating with them. Now, I have useful classes that decrease my future development time when this type of work is required. I am also able now to easily parse RSS feeds that are used all over the Web. I am planning to extend this basic support to HTML, or XML documents that are not-so-well-formed, soon (when I find some more free time).

License

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

Share

About the Author

darkoman
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
samcsu18-Apr-11 17:20
membersamcsu18-Apr-11 17:20 
GeneraltinyXML Pin
As_Sanya15-Dec-10 21:41
memberAs_Sanya15-Dec-10 21:41 
AnswerRe: tinyXML Pin
darkoman29-Dec-10 20:04
memberdarkoman29-Dec-10 20:04 
GeneralOne type of tag is not handled... Pin
bala66629-Nov-10 23:18
memberbala66629-Nov-10 23:18 
AnswerRe: One type of tag is not handled... Pin
darkoman12-Dec-10 9:20
memberdarkoman12-Dec-10 9:20 
GeneralMy vote of 5 Pin
RajaManikandan_R23-Sep-10 18:15
memberRajaManikandan_R23-Sep-10 18:15 
GeneralUsing nested elements with the same names - unable to load file Pin
GYuval6-Jul-10 21:55
memberGYuval6-Jul-10 21:55 
GeneralA couple of problems Pin
LEKV16-Feb-10 5:17
memberLEKV16-Feb-10 5:17 
AnswerRe: A couple of problems Pin
darkoman16-Feb-10 9:25
memberdarkoman16-Feb-10 9:25 
GeneralRe: A couple of problems Pin
LEKV16-Feb-10 12:42
memberLEKV16-Feb-10 12:42 
GeneralRe: A couple of problems Pin
LEKV17-Feb-10 4:44
memberLEKV17-Feb-10 4:44 
Generalnow i saved to file, and Pin
liaterez18-Nov-09 3:43
memberliaterez18-Nov-09 3:43 
Generalsorry for confused (- 1 and 4 are the same) Pin
liaterez18-Nov-09 3:44
memberliaterez18-Nov-09 3:44 
Generalxml reader performance Pin
liaterez17-Nov-09 5:01
memberliaterez17-Nov-09 5:01 
AnswerRe: xml reader performance Pin
darkoman17-Nov-09 10:43
memberdarkoman17-Nov-09 10:43 
Generalnice but useless for the rest of us Pin
rajeshFeb028-Apr-09 22:39
memberrajeshFeb028-Apr-09 22:39 
GeneralRe: nice but useless for the rest of us Pin
darkoman15-Apr-09 19:49
memberdarkoman15-Apr-09 19:49 
GeneralRe: XML Mis-parsing [modified] Pin
Member 603819625-Mar-09 22:59
memberMember 603819625-Mar-09 22:59 
GeneralXML Mis-parsing Pin
owl-len11-Mar-09 13:01
memberowl-len11-Mar-09 13:01 
GeneralRemove nodes from file [modified] Pin
bijumanjeri8-Jan-09 3:03
memberbijumanjeri8-Jan-09 3:03 
GeneralCan't parser the XML file Pin
gu@z23-Nov-08 16:16
membergu@z23-Nov-08 16:16 
GeneralRe: Can't parser the XML file Pin
darkoman25-Nov-08 6:10
memberdarkoman25-Nov-08 6:10 
QuestionIs this a MFC dependent? Can you make it ANSI C/C++ class? Pin
kedanz8-May-08 3:43
memberkedanz8-May-08 3:43 
AnswerRe: Is this a MFC dependent? Can you make it ANSI C/C++ class? Pin
darkoman8-May-08 6:45
memberdarkoman8-May-08 6:45 
GeneralRe: Is this a MFC dependent? Can you make it ANSI C/C++ class? Pin
Dallin Wellington3-Sep-08 16:53
memberDallin Wellington3-Sep-08 16:53 
AnswerRe: Is this a MFC dependent? Can you make it ANSI C/C++ class? Pin
darkoman4-Sep-08 22:46
memberdarkoman4-Sep-08 22:46 
GeneralProblem while creating new elements Pin
noh_se_nada8-Apr-08 6:52
membernoh_se_nada8-Apr-08 6:52 
AnswerRe: Problem while creating new elements Pin
darkoman8-Apr-08 12:31
memberdarkoman8-Apr-08 12:31 
GeneralRe: Problem while creating new elements Pin
noh_se_nada9-Apr-08 5:08
membernoh_se_nada9-Apr-08 5:08 
QuestionDoes CXMLFile can handle UTF-8 encoding? Pin
Homero De la Garza4-Apr-08 10:19
memberHomero De la Garza4-Apr-08 10:19 
GeneralRe: Does CXMLFile can handle UTF-8 encoding? Pin
darkoman4-Apr-08 13:20
memberdarkoman4-Apr-08 13:20 
GeneralRe: Does CXMLFile can handle UTF-8 encoding? Pin
Homero De la Garza5-Apr-08 8:38
memberHomero De la Garza5-Apr-08 8:38 
GeneralMisc ¨Questions Pin
noh_se_nada28-Mar-08 0:42
membernoh_se_nada28-Mar-08 0:42 
GeneralRe: Misc ¨Questions Pin
darkoman28-Mar-08 7:38
memberdarkoman28-Mar-08 7:38 
GeneralRe: Misc ¨Questions Pin
noh_se_nada30-Mar-08 23:29
membernoh_se_nada30-Mar-08 23:29 
GeneralRe: Misc ¨Questions Pin
noh_se_nada1-Apr-08 5:35
membernoh_se_nada1-Apr-08 5:35 
GeneralRe: Misc ¨Questions Pin
darkoman1-Apr-08 11:19
memberdarkoman1-Apr-08 11:19 
GeneralTAB processing Pin
OHara27-Mar-08 23:15
memberOHara27-Mar-08 23:15 
GeneralRe: TAB processing [modified] Pin
noh_se_nada10-Apr-08 1:14
membernoh_se_nada10-Apr-08 1:14 
QuestionSimple C++ XML parser Pin
RobMenegon25-Mar-08 12:45
memberRobMenegon25-Mar-08 12:45 
GeneralRe: Simple C++ XML parser [modified] Pin
darkoman25-Mar-08 13:00
memberdarkoman25-Mar-08 13:00 
GeneralRe: Simple C++ XML parser Pin
brianma25-Mar-08 13:20
memberbrianma25-Mar-08 13:20 
GeneralRe: Simple C++ XML parser Pin
darkoman26-Mar-08 0:54
memberdarkoman26-Mar-08 0:54 
QuestionRe: Simple C++ XML parser Pin
amach23-Oct-08 6:53
memberamach23-Oct-08 6:53 
AnswerRe: Simple C++ XML parser Pin
darkoman23-Oct-08 9:15
memberdarkoman23-Oct-08 9:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 19 Mar 2008
Article Copyright 2008 by darkoman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid