LibXML2 is a XML C parser and toolkit written by by Daniel Veillard for the GNOME project. It is open source and works on a variety of systems.

And to get this to work on windows you can download the source and compile it yourself or download the prebuild windows binarys of it here.
And since the LibXML2 is a C Toolkit it is a bit messy to use for C++ developers. And that is where my helper class comes in. CLibXMLHelper is a C++ wrapper class that will help you to work with XML files using LibXML2.

LibXML2 uses UTF8 internally so all string send to the API should be UTF8. This helper class take care of this.


  • LibXML2 (ofcourse).
    You need to have LIBXML2 SDK installed and built before you can use this helper.
  • zSTLDef.h and UTF8Conv.h/UTF8Conv.cpp are two generic helper files I also have created. They are included in the download package.
  • StringParser is a class that parse strings that are very good, It is created by Remon and was found on (included in the download)


Make sure you have setup your project so it links to LibXML.

Then add StringParser.cpp/.h, UTF8Conv.cpp/.h and LibXMLHelper.cpp/.h to your solution. and it is recommended is to include zSTLDef.h and UTF8Conv.h to stdafx.h must not absolutely necessary

In files you want to use the LibXML Helper include the LibXmlHelper header file.

#include "LibXMLHelper.h"


Create XML file and add 10 child items.

xmlDocPtr doc = NULL;
xmlNodePtr rootNode = NULL;
xmlNodePtr node = NULL;
xmlNodePtr nodeFound = NULL;

doc = xmlNewDoc( (const xmlChar*)"1.0" );
rootNode = xmlNewNode( NULL , (const xmlChar*)"config" );
xmlDocSetRootElement( doc , rootNode );

// Add 10 Sub Items
for( int n = 0 ; n < 4 ; n++ )
  CString str;
  str.Format("configitem%d" , n );
  node = CLibXMLHelper::CreateChildElement( rootNode , (const char*)str );
  if( node )
    CLibXMLHelper::SetValue( nodePtr , "value" , n ));

// the å ä ö Å Ä Ö will be converted to UTF-8 by the helper class
node = CLibXMLHelper::CreateChildElement( rootNode , "UserInfo" ); 
node = CLibXMLHelper::CreateChildElement( node , "user"  , "user information. åäöÅÄÖ" ); 
if( node )
  CLibXMLHelper::SetValue( node , "name" , "mathias" );
  CLibXMLHelper::SetValue( node , "site" , "");
// Save the xml file with UTF-8 encoding
int Ret = xmlSaveFormatFileEnc("LibXmlTest.xml", doc, "UTF-8" , 1 ); 
if( Ret == -1 )
  ::MessageBox( NULL , _T("Error saving xmlfile!") , _T("Error") , MB_OK |MB_ICONERROR );

xmlFreeDoc( doc );


 <configitem1 value="1"  />
 <configitem2 value="2"  />
 <configitem3 value="3"  />
 <configitem4 value="4"  />
  <user name="mathias" site="">user information. åäöÅÄÖ</user>


 1.2 - 13-Sep-06 - Added GetElementName( xmlNodePtr node )
 1.3 - 19-Okt-06 - Added SetValues with 'const TCHAR*' alternatives
 1.4 -  6-Feb-07 - Fixed SetContent. It was handled wrong. 
                   And all child tag would go away if setContent was used and tag had subtags.
                   (Should revisit this later and make it more smart.)

 1.5 - 18-May-07 - Added FindElement/RemoveElemet with a map of attribute and value to match against
 1.6 - 15-Jun-07 - Added SetValue for attribute of type int ( got tired casting to long )
       15-Jun-07 - Added Get/SetValueColor for COLORREF

 1.7    2-Jul-07 - Fixed memory leak in GetElementText();
 1.7.1  8-Okt-08 - Fixed minor problem with GetElementPath();

 1.8.0 13-Nov-08 - Added GetMultiValueStr();
 1.9.0 10-Feb-10 - Added RemoveElement to remove a Node. 
                   also some minor refactoring to the code