Simple XML Parsing on WinCE 4.2 Using C++ and MSXML 3.0

This article will show you how to parse XML files on WinCE using MSXML 3.0, a XML parser from Microsoft.

Okay, there are many articles around showing you how to parse XML, but not that many for WinCE using Visual C++. A lot of examples are for the .NET environment using Visual Basic .NET and C#. I’d like to share my experience here with parsing XML on Windows CE 4.2. The classes I present can be run on any device that has WinCE4.2 with MSXML 3.0 installed.

Please note that I am using MSXML 3.0 as of writing this article; this is the latest version supported by WinCE 4.2. WinXP/Win2K supports version 4.0 that includes updated interfaces and supports more XPath expressions.

The prerequisites for this article are that you have some experience with XML and the ways in which it can be manipulated using DOM or SAX. This article uses the former. Also, some experience of COM would be helpful.

A Brief Overview of DOM (Document Object Model)

DOM presents an XML document as a tree structure, just like a file hierarchy in Windows Explorer. The tree has a root node, the root node has parent nodes, the parent nodes have child nodes—I think you get the picture. You can refer to these nodes as elements, with other elements embedded inside them. These elements contain text and attributes that are manipulated through this DOM tree. The contents of these elements can be modified or deleted, and we can create new elements.

MSXML—Microsoft’s XML Parser

MSXML is based on COM; it comes with Internet Explorer 5 and above. This component has many functions that help you traverse the XML document, access nodes within it, delete these nodes, update these nodes, insert nodes, and more. It is worth noting that MSXML also supports XSLT and XPath. I won’t be using these technologies in this article, but just so you know, these are supported.

That was a brief description of DOM and MSXML. If you need to know more, the Internet is a great resource that has many articles on the aforementioned topics.

Initialising MSXML

Now, on to some code. The first thing you need to do to use MSXML is to initialise it, Remember, I mentioned earlier that this is a COM component, so you first need to initialise COM:


This will return S_OK if all is well.

You now need to create an instance of the MSXML COM object. I do the following for this:

hr = m_iXMLDoc.CoCreateInstance( __uuidof( DOMDocument ) );

So, what is m_iXMLDoc I hear you ask? It is of type IXMLDOMDocument and represents the top level of the XML document. It is worth pointing out now that WinCE4.2 only supports MSXML 3.0. If you were using Microsoft Windows XP, for example, this supports MSXML 4.0; thus, we could use IXMLDOMDocument4, which supports other methods.

I have wrapped m_iXMLDoc up into a ATL smart pointer. This is done to avoid having to release the object myself (I may forget!); thus, you have:

CComPtr<IXMLDOMDocument> m_iXMLDoc;

If this function call succeeds, it will return S_OK.

The next bit of code looks quite odd but is needed only if you are using Pocket PC:

CComQIPtr<IObjectSafety,&IID_IObjectSafety> iSafety( m_iXMLDoc );
if ( iSafety )
   DWORD dwSupported, dwEnabled;
   iSafety->GetInterfaceSafetyOptions( IID_IXMLDOMDocument,
                                       &dwSupported, &dwEnabled

  iSafety->SetInterfaceSafetyOptions( IID_IXMLDOMDocument,
                                      dwSupported, 0 );

This was taken off the Internet. I can’t remember from where, so apologies to the person who wrote this, but without it, things don’t seem to work; it is needed to mark the MSXML control as safe.

Loading the XML

You now have initialised COM and created the MSXML object; this in turn now lets us use the functionality supplied by the MSXML object. I am now going to load a very basic XML file. It looks like the following:

      <customer name="OnLineGolf" tag="OLG"/>
      <customer name="BetterGolf" tag="BG"/>

It’s very simple. There isn’t a lot to this, just a couple of elements and a couple of attributes. So, load this document. This is done with the following code:

VARIANT_BOOL bSuccess=false;
hr = m_iXMLDoc->load( CComVariant ( szXMLFile ), &bSuccess );

szXMLFile is the name of the XML file you want to load. Please note that this could easily be a file that resides on a Web server; thus, you could specify a file URL. bSuccess will contain true if all is well.

Now, before moving on to the next bit of code, I need to introduce you to a useful function I wrote:

void CCEXML::DisplayChildren( IXMLDOMElement* pParent )

This function is going to traverse an element/node recursively. It looks like this:

void CCEXML::DisplayChildren( IXMLDOMElement* pParent )
   static IXMLDOMNode* pNextSib = NULL;
   static IXMLDOMNode* pChild   = NULL;

   if( pParent == NULL )    // Finished child

   DisplayChild( pParent );

      pNextSib = pChild;
      pParent->get_firstChild( &pChild );
      if( pChild == NULL )
         pNextSib->get_nextSibling( &pChild );

      DisplayChildren( (IXMLDOMElement*)pChild );
   while( pChild != NULL );


Within this function is another important function:

void CCustomerXML::DisplayChild(IXMLDOMElement* pChild)

This is a pure virtual function. This means the user of your class needs to implement this function, More on this later.

Okay, back to the code. If the document has been loaded successfully, you can start to traverse it. This is done with the following piece of code:

CComPtr<IXMLDOMElement> iRooterElm;
hr = m_iXMLDoc->get_documentElement( &iRooterElm );
if( FAILED( hr ) || iRooterElm == NULL )    // Empty xml file
   MessageBox( NULL, L"Empty document!", L"Error Loading XML",
               MB_ICONSTOP );
   return FALSE;

IXMLDOMNode* iNode    = NULL;
IXMLDOMNodeList* List = NULL;
iRooterElm->get_childNodes( &List );

long Amount;
List->get_length( &Amount );
for( int i = 0; i < Amount; i++ )
   List->get_item( i, &iNode );
   DisplayChildren( (IXMLDOMElement*)iNode );

By using this and the DisplayChildren function, the whole of the document is traversed.

More by Author

Must Read