Click here to Skip to main content
11,790,904 members (66,357 online)
Click here to Skip to main content

C++ XML the easy way!

, 10 Jun 2005 CPOL 645K 10.1K 139
Rate this:
Please Sign up or sign in to vote.
A wrapper for the XML DOM for C++.


This article is a successor to my previous article Wrapper class for IXMLDDOMDocument.

The reason that it's published as a different article and not an update is that, it's in no way compatible with the previous one. There are different classes and functions. Also the main difference is that this wrapper works directly with XML pointers without first parsing the XML tree, which leads to a significant speed improvement.


Add the file "XmlNodeWrapper.cpp" to your project's source files.

Include the file "XmlNodeWrapper.h" in the files you would like to use the wrapper.

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



  • CXmlDocumentWrapper()

    Creates a new instance of XML document. You have to load a document via Load() or LoadXml()

  • CXmlDocumentWrapper(IXMLDOMDocumentPtr pDoc)

    Attaches the wrapper to the existing document pointer. Will accept as input an IXMLDOMDocumentPtr, IXMLDOMDocument* pointer or IDispatch* pointer.

  • BOOL IsValid()

    Returns TRUE if the wrapper has a valid pointer to XML document.

  • MSXML2::IXMLDOMDocument* Detach()

    Detaches the wrapper from the XML pointer and returns the pointer. Useful if you passed the external pointer in the constructor and would not like to decrease the reference count on the pointer.

  • MSXML2::IXMLDOMDocument* Clone()

    Creates and returns a clone of the document.

    Suggested usage:

    CXmlDocumentWrapper newDoc(sourceDoc.Clone());
  • BOOL Load(LPCTSTR path)

    Loads a document from a disk file. Returns TRUE on success.


    CXmlDocumentWrapper doc;

    Load the document from a string. Returns TRUE on success.


    CXmlDocumentWrapper doc;
  • BOOL Save(LPCTSTR path = "")

    Saves the document to a disk file. If the path is an empty string or omitted, the current document URL is used.

  • MSXML2::IXMLDOMNode* AsNode()

    Returns the document root element as XML node.


    CXmlNodeWrapper xmlNode(xmlDoc.AsNode());
  • CString GetXML()

    Returns the current XML loaded in the document as a CString.

  • CString GetUrl()

    Returns the current document URL. Note: The URL is updated every time you use Load() or Save() functions.


  • CXmlNodeWrapper()

    Creates an empty Node wrapper. You have to use operator = or Attach() on this node.

  • CXmlNodeWrapper(MSXML2::IXMLDOMNodePtr pNode,BOOL bAutoRelease = TRUE)

    Creates a wrapper from XMl pointer to existing XML node. Will accept IXMLDOMNodePtr, IXMLDOMNode* or IDispatch* pointer. If the bAutoRelease parameter is FALSE,Detach() will be called in the destructor.

  • void operator=(MSXML2::IXMLDOMNodePtr pNode)

    Same as the above.

  • BOOL IsValid()

    Returns TRUE if the XML pointer in the wrapper is valid.

  • void SetValue(LPCTSTR valueName,LPCTSTR value)
  • void SetValue(LPCTSTR valueName,int value)
  • void SetValue(LPCTSTR valueName,short value)
  • void SetValue(LPCTSTR valueName,double value)
  • void SetValue(LPCTSTR valueName,float value)
  • void SetValue(LPCTSTR valueName,bool value)

    Sets a value for an attribute. If the attribute exists it's value is updated, otherwise a new attribute will be created.

  • CString GetValue(LPCTSTR valueName)

    Returns a value of an attribute as a CString. If the attribute does not exists, an empty string will be returned.

  • MSXML2::IXMLDOMNode* GetPrevSibling()
  • MSXML2::IXMLDOMNode* GetNextSibling()

    Gets the next or previous sibling of this node. If no such exists NULL will be returned.


    CXmlNodeWrapper prevNode(theNode.GetPrevSibling());
    CXmlNodeWrapper nextNode(theNode.GetNextSibling());
    if (prevNode.IsValid())
          // Do things
    else if (nextNode.IsValid())
         // Do things
  • MSXML2::IXMLDOMNode* GetNode(LPCTSTR nodeName)
  • MSXML2::IXMLDOMNode* GetNode(int nodeIndex)

    Returns a child node of this node by index or by name. If no node is found, NULL will be returned. When searching by name only the first matching node will be returned.


    CXmlNodeWrapper childNode(theNode.GetNode(0));
    if (!childNode.IsValid())
         childNode = theNode.GetNode("MyChild");
         // Do things
  • MSXML2::IXMLDOMNode* FindNode(LPCTSTR searchString)

    Returns a single node by evaluating an XSL search pattern (A wrapper for selectSingleNode function).


    //Finds the node with the name "Object"
    // who's "Type" attribute equals "Simple":
    CXmlNodeWrapper resultNode(
  • MSXML2::IXMLDOMNodeList* FindNodes(LPCTSTR searchStr)

    Returns a list of nodes by evaluating an XSL search pattern (A wrapper for selectNodes function).


    //Finds all the nodes with the name "Object" 
    //who's "Type" attribute equals "Simple":
    CXmlNodeListWrapper nodeList(
  • long NumNodes()

    Returns the number of child nodes.

  • MSXML2::IXMLDOMNode* Parent()

    Return a parent node of this node or NULL if none.


    CXmlNodeWrapper parentNode(theNode.Parent());
  • CString Name();

    Returns the name of the node.

  • CString GetText()

    Returns the text contents of the node.

  • void SetText(LPCTSTR text)

    Sets the text contents of the node.

  • MSXML2::IXMLDOMDocument* ParentDocument()

    Returns the document that this node belongs to or NULL if not applicable.


    CXmlDocumentWrapper xmlDoc(theNode.ParentDocument());
  • MSXML2::IXMLDOMNode* Interface()

    Returns the raw interface pointer, useful to invoke some of the methods not provided by the wrapper or to pass the interface to some other functions without detaching from the wrapper.

  • MSXML2::IXMLDOMNode* Detach()

    Detaches from the XML pointer without calling Release() on it and returns the pointer.

  • CString GetXML()

    Returns the XML contents of the node as CString.

  • MSXML2::IXMLDOMNode* InsertNode(int index,LPCTSTR nodeName)

    Creates a new XML node with the given name and inserts it as a child of this node. Use index=0 to insert as a first child and index=NumNodes() to insert as the last. Returns a pointer to the new node.


    CXmlNodeWrapper newChild(
  • MSXML2::IXMLDOMNode* InsertNode(int index,MSXML2::IXMLDOMNodePtr pNode)

    Same as above. Only the new child is not created but accepts a pre-existing pointer to a node.


    // Create a copy of a document 
    //and insert as a child into existing node
    CXmlDocumentWrapper doc;
    CXmlNodeWrapper sourceNode(doc.AsNode());
    CXmlDocumentWrapper cloneDoc(doc.Clone());
  • MSXML2::IXMLDOMNode* InsertAfter(MSXML2::IXMLDOMNode* refNode, LPCTSTR nodeName)
  • MSXML2::IXMLDOMNode* InsertBefore(MSXML2::IXMLDOMNode* refNode, LPCTSTR nodeName)

    Create and insert a new child node with the given name before or after the referenced node. Returns the newly created node.

  • MSXML2::IXMLDOMNode* InsertAfter(MSXML2::IXMLDOMNode *refNode, MSXML2::IXMLDOMNode *pNode)

    Insert an existing node after the referenced node. Returns the inserted node.

  • MSXML2::IXMLDOMNode* RemoveNode(MSXML2::IXMLDOMNodePtr pNode)

    Removes the specified node from the child list of this node. Returns a pointer to the removed node.

  • void RemoveNodes(LPCTSTR searchStr)

    Removes nodes from the child list of this node by evaluating XSL pattern. See FindNodes for an example.

  • void ReplaceNode(MSXML2::IXMLDOMNode* pOldNode,MSXML2::IXMLDOMNode* pNewNode)

    Replaces an existing child node with a new node.


  • CXmlNodeListWrapper()

    Creates an empty node list wrapper, you have to use the operator = to initialize it.

  • CXmlNodeListWrapper(MSXML2::IXMLDOMNodeListPtr pList)

    Creates a node list from an existing IXMLDOMNodeList pointer, will accept IXMLDOMNodeListPtr,IXMLDOMNodeList* pointer or IDispatch* pointer. See example in CXmlNodeWrapper::FindNodes().

  • void operator=(MSXML2::IXMLDOMNodeListPtr pList)

    Same as above.

  • BOOL IsValid()

    Returns TRUE if the list pointer is valid.

  • int Count()

    Returns the number of nodes in the list.

  • MSXML2::IXMLDOMNode* Node(int index)

    Return the nth node in the list.


    for (int i = 0; i < nodeList.Count(); i++)
         CXmlNodeWrapper node(nodeList.Node(i));
         // Do things
  • void Start()

    Resets the iterator. See example in Next().

  • MSXML2::IXMLDOMNode* Next()

    Return the next node in the list when iterating or NULL if none.


    CXmlNodeWrapper node(nodeList.Next());
    while (node.IsValid())
        // Do things
        node = nodeList.Next();
  • MSXML2::IXMLDOMDocument* AsDocument()

    Converts the node list to a new document in the form: <NodeList>THENODES</NodeList>


    CXmlDocumentWrapper doc(theList.AsDocument());


The sample is now available for download. Also a few more new functions are included:

  • int CXmlNodeWrapper::NumAttributes() - returns the number of attributes in the node.
  • CString CXmlNodeWrapper::NodeType() - returns the type of the node : "element", "text", etc.
  • CString CXmlNodeWrapper::GetAttribName(int index) - returns the name of the attribute at index.
  • CString CXmlNodeWrapper::GetAttribVal(int index) - returns the value of the attribute at index.


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


About the Author

Alex Hazanov
Software Developer (Senior) RDV Systems
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

BugLoadXML throws exception when xml string is too long! Pin
vktinh27-Jul-15 20:21
membervktinh27-Jul-15 20:21 
QuestionIncorrect casts when used in a Unicode environment Pin
m3driver3-Nov-14 12:48
memberm3driver3-Nov-14 12:48 
GeneralMy vote of 5 Pin
Member 43419454-Mar-13 23:02
memberMember 43419454-Mar-13 23:02 
QuestionHow to creat new XML documents Pin
minhasp27-Nov-12 17:25
memberminhasp27-Nov-12 17:25 
QuestionWhat changes should be done in the XML Pin
Tarun Batra16-Oct-12 20:04
memberTarun Batra16-Oct-12 20:04 
QuestionI dont being able to use FindNode and SetText Pin
Rogerio Torres3-Oct-12 7:28
memberRogerio Torres3-Oct-12 7:28 
Questionusing your wrapper in a win32 program? Pin
SimonNaus12-Jun-12 1:56
memberSimonNaus12-Jun-12 1:56 
AnswerRe: using your wrapper in a win32 program? Pin
Alex Hazanov12-Jun-12 2:56
memberAlex Hazanov12-Jun-12 2:56 
Take a look at a newer article at experts-exchange:[^]

Creative minds - create creative creations!

GeneralRe: using your wrapper in a win32 program? Pin
SimonNaus12-Jun-12 4:33
memberSimonNaus12-Jun-12 4:33 
GeneralRe: using your wrapper in a win32 program? Pin
Divya Rathore12-Jul-12 16:30
memberDivya Rathore12-Jul-12 16:30 
Generalchecking well formed item name Pin
gok9-Feb-11 5:19
membergok9-Feb-11 5:19 
GeneralMove the node up and down Pin
Binh Duong Thanh5-Oct-10 23:10
memberBinh Duong Thanh5-Oct-10 23:10 
GeneralMy vote of 1 Pin
blaabcdefghijk7-Apr-10 3:21
memberblaabcdefghijk7-Apr-10 3:21 
GeneralAnother Approach Pin
Brian Aberle11-Feb-10 19:13
memberBrian Aberle11-Feb-10 19:13 
QuestionHow to load UTF-8 string from XML files correctly. Pin
Vincent-Lin9-Dec-09 15:04
memberVincent-Lin9-Dec-09 15:04 
GeneralInsertNode -&gt; Error Pin
Magnus Persson14-Oct-09 2:42
memberMagnus Persson14-Oct-09 2:42 
GeneralCXml was much easier to use for my needs Pin
Snakefoot24-Sep-09 23:44
memberSnakefoot24-Sep-09 23:44 
Generalfopen_s fails after using CXmlDocumentWrapper Pin
fisyher23-Aug-09 20:20
memberfisyher23-Aug-09 20:20 
QuestionGetText() does not return the whitespace Pin
bluebluesky669931-Jul-09 0:48
memberbluebluesky669931-Jul-09 0:48 
QuestionReferences of XML Pin
defenser18-Jul-09 21:04
memberdefenser18-Jul-09 21:04 
GeneralThank you very much Pin
kira711-Jul-09 8:03
memberkira711-Jul-09 8:03 
Questionhow to release the memory after using it? Pin
qzb20067-Apr-09 17:12
memberqzb20067-Apr-09 17:12 
AnswerRe: how to release the memory after using it? Pin
Whuili_20046-Feb-12 14:20
memberWhuili_20046-Feb-12 14:20 
AnswerRe: how to release the memory after using it? Pin
vktinh18-Jun-15 22:32
membervktinh18-Jun-15 22:32 
AnswerRe: how to release the memory after using it? Pin
vktinh22-Jun-15 0:18
membervktinh22-Jun-15 0:18 

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
Web04 | 2.8.1509028.1 | Last Updated 10 Jun 2005
Article Copyright 2005 by Alex Hazanov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid