DOMErrorHandling C-C++ example


The following C/C++ example attempts to load an invalid XML document. The parser encounters an error and uses pXMLDoc->parseError->reason to get the error message. For more information on other properties of the parseError object, see IXMLDOMParseError Members.

Source Listing for DOMErrorHandling.cpp

When you created DOMErrorHandling project, Microsoft Visual Studio created the file DOMErrorHandling.cpp. Modify DOMErrorHandling.cpp so that it contains the code in the listing below. Build the project.

#include "stdafx.h"
#import <msxml6.dll>
using namespace MSXML2;

int main(int argc, char* argv[])
   HRESULT hr;
   IXMLDOMDocument2Ptr pXMLDoc;


   hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));
   if (FAILED(hr)) 
      printf("Failed to CreateInstance on an XML DOM");
      return -1;
   pXMLDoc->async = VARIANT_FALSE; 
   if(pXMLDoc->load("books.xml")!= VARIANT_TRUE)
      return -1;


   return 0;

XML Data File (books.xml)

Copy the data from Resource: books.xml for DOMErrorHandling example and save it as books.xml. This file should be saved in the Debug directory that was created after you built DOMErrorHandling project.


When you run the DOMErrorHandling project, you should get the following output in a console window:

End tag 'book' does not match the start tag 'review'.