Source: loadDOMRaw.cpp

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Source: loadDOMRaw.cpp

This C/C++ source code performs the following steps:

  1. Creates an XML DOM object (pXMLDom) and sets it to synchronous mode.
  2. Calls the load method on pXMLDom, specifying the path to stocks.xml.

C/C++ Source File (loadDomRaw.cpp)

#include <stdio.h>
#include <msxml2.h>

// You might need to add the msxml5/sdk/(inc, lib) directories 
// to the Tools->Options...->Directories in Visual Studio.
//
// You might also need to append "msxml2.lib" to the
// Project->Settings...->Link->Object/Libray Modules field.

int main(int argc, char* argv[])
{
   HRESULT hr;
   IXMLDOMDocument3 *pXMLDoc = NULL;
   IXMLDOMParseError * pObjError = NULL;
   BSTR bstr = NULL;
   VARIANT_BOOL status;
   VARIANT vSrc;

   CoInitialize(NULL);

   hr = CoCreateInstance(CLSID_DOMDocument50, 
                     NULL, 
                     CLSCTX_INPROC_SERVER, 
                     IID_IXMLDOMDocument3,
                     (void**)&pXMLDoc);

   if (FAILED(hr)) 
   {
      printf("Failed to CoCreate an instance of an XML DOM\n");
      printf("Error code: %x\n", hr);
      goto clean;
   }

   hr = pXMLDoc->put_async(VARIANT_FALSE);
   if (FAILED(hr))
   {
      printf("Failed to set async property\n");
      goto clean;
   }

   hr = pXMLDoc->put_validateOnParse(VARIANT_FALSE);
   if (FAILED(hr))
   {
      printf("Failed to set validateOnParse\n");
      goto clean;
   }

   hr = pXMLDoc->put_resolveExternals(VARIANT_FALSE);
   if (FAILED(hr))
   {
      printf("Failed to disable resolving externals.\n");
      goto clean;
   }

   VariantInit(&vSrc);
   V_BSTR(&vSrc) = SysAllocString(L"stocks.xml");
   V_VT(&vSrc) = VT_BSTR;

   hr = pXMLDoc->load(vSrc, &status);

   if(status!=VARIANT_TRUE)
   {
      hr = pXMLDoc->get_parseError(&pObjError);
      hr = pObjError->get_reason(&bstr);
      printf("Failed to load DOM from books.xml. %S\n",bstr);
      goto clean;
   }

   hr = pXMLDoc->get_xml(&bstr);
   printf("stocks.xml:\n%S\n", bstr);

clean:   
   if (bstr)
      SysFreeString(bstr);
   if (&vSrc) 
      VariantClear(&vSrc);
   if (pObjError)
      pObjError->Release();
   if (pXMLDoc)
      pXMLDoc->Release();
   CoUninitialize();
   return 0;

}

To add loadDOMRaw.cpp to the project

  1. Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the new file loadDOMRaw.cpp.
  2. Copy the C/C++ source code above and paste it into the source file you just created.

Next, we'll add the resource file, stocks.xml.