Using TinyXML2 for XML Parsing in C++

I was searching for a easy,fast and small XML Parser for my project when I came across TinyXML2. I tried it and found that its indeed an easy and clean little parser which can cater to my need excellently.

Now lets see how it works.

Setting Up

You can get it from :

Just download it and build the files using the solution available in the folder. Then you will get Object file(tinyxml.obj) in “tinyxml2-master\tinyxml2\Win32\Release” or “tinyxml2-master\tinyxml2\Win32\Debug” depends up on which configuration you built.

You will have to link the object file in your project settings or else you will get “Unresolved external symbol errors”.

Include “tinyxml2.h” in your C++ file.

Now we are ready to Tackle XMLs.

Load and Parse XML File

Create a XMLDocument Object and call LoadFile() function. Give the file path as its parameter.

XMLDocument xmlDoc;


Parse XML formatted string

If you have a string which contains an XML, You can parse that using XMLDocument’s parse function.

XMLDocument dxml;

//Parse the xml sxml 
int rslt = dxml.Parse(sxml.c_str());

Getting root node

You can get the root node by calling FirstChild function of XMLDocument. It will return a pointer to the XMLNode class

XMLNode *rootnode = dxml.FirstChild();

Iterating Sibling Nodes

Then to iterate the nodes in the same level call NextSibling() function of XMLNode

XMLNode SiblingNode = rootnode ->NextSibling();

Getting Child Node

To get down to the child level nodes of current node call FirstChild() function of XMLNode

ChildNode = SiblingNode ->FirstChild();

Getting Data from Nodes

To get data from nodes we need to get the xml element from the Node. XMLNode class provides ToElement() function for this operation.

XMLElement *ChildElmt = ChildNode->ToElement();

Then we can get the Tag name of the Node using Name() function of XMLElement class.

string strTagName = ChildElmt ->Name();

We can get the values of the attributes of elements using the Attribute() function by giving the attribute name as parameter.

string strAttr = ChildElmt ->Attribute("YourAttributeName");

I am using tinyxml for some time now. And I must say I love its simplicity..

why we use Virtual Destructor

A destructor is a member function of a class, which gets called when the object goes out of scope.  So we have to do  all clean ups and final steps of class destruction are to be done in destructor.

If a member function is virtual, then calling the function on an object always invokes the function as implemented by the class which is derived finally. If the method is not virtual, then the implementation corresponding to the compile-time type of the object pointer.


Class Base
Base(){cout<<”Base constructor”;}
~Base{cout<<”Base destructor”;}
Class Derived: public Base
Derived(){cout<<”Derived constructor”;}
~Derived{cout<<”Derived destructor”;}
void main()
Base *Obj = new Derived();
delete Obj;

The output will be


Base constructor
Derived constructor
Base destructor

From the output we can see that the derived class destructor is not getting evoked. This is where virtual function comes to our aid. If we make the base class destructor as virtual.

Class Base
    Base(){cout<<"Base constructor";}
    virtual ~Base{cout<<"Base destructor";}

We will get our desired output as

Base constructor
Derived constructor
Derived destructor
Base destructor

how to use QT QProgressBar to show busy/indefinite status

A progress bar is used to give the user an indication of the progress of an operation and to reassure them that the application is still running.

The progress bar uses the concept of steps. You set it up by specifying the minimum and maximum possible step values, and it will display the percentage of steps that have been completed when you later give it the current step value.

In QT we can use QProgressBar widget which provides a horizontal or vertical progress bar for the same.

To show busy or indefinite time in progress bar using QT, just set the maximum and minimum limits of QProgressBar to 0.

QProgressBar *pbarDL;
//To start indefinite/busy progress bar
//To stop just set any value to maximum
And you will get
QProgressBar showing Busy status

QProgressBar showing Busy status

That’s it.. Easy enough na..Winking smile

XRC – The XML Based Resource System in wxWidgets

XRC allows user interface elements such as dialogs, menu bars and toolbars, to be stored in text files and loaded into the application at run-time.

XRC files can be compiled in to two formats.
    1. XRS : To store all resources in a single file.
    2. C++ : To embed resource into executable.

Why do we use XRC

  • Resource change doesn’t need recompiling and linking.
  • Separation of resources and code.
  • You can choose between different alternative resource files at run time, if necessary.
  • The XRC format uses sizers for flexibility, allowing dialogs to be resizable and highly portable.
  • The XRC format is a wxWidgets standard, and can be generated or postprocessed by any program that understands it. As it is based on the XML standard, existing XML editors can be used for simple editing purposes.

XRC Basics

  • Include Header : wx/xrc/xmlres.h.
  • wxFileSystem archive handler is needed for XRS files. Install wxFileSystem archive handler first with wxFileSystem::AddHandler(new wxArchiveFSHandler);
  • To load resource file
    • From your wxApp::OnInit() function call wxXmlResource::Get()->InitAllHandlers()
    • Then call wxXmlResource::Get()->Load(“myfile.xrc”)
  • to create a dialog from a resource, create it using the default constructor, and then load it. For example wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
  • set up event tables as usual but use the XRCID(str) macro to translate from XRC string names to a suitable integer identifier, for exampleEVT_MENU(XRCID("quit"), MyFrame::OnQuit).
XRC File Creation

To create an XRC file, you can use one of the following methods.

  • Create the file by hand;
  • use wxFormBuilder, Open source designer supporting C++, Python and XRC.
  • use DialogBlocks, a commercial dialog editor;
  • use XRCed, a wxPython-based dialog editor that you can find in the wxPython/tools subdirectory of the wxWidgets CVS archive;
  • use wxGlade, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
  • convert WIN32 RC files to XRC with the tool in contrib/utils/convertrc.

A complete list of third-party tools that write to XRC can be found at

You can use wxXmlResource::Load in a number of ways. You can pass an XRC file (XML-based text resource file) or a zip-compressed file (extension ZIP or XRS) containing other XRC.