Share via


Source: loadDOMsmart.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 (loadDOMsmart.cpp)

#include <stdio.h>
#include <tchar.h>
#import <msxml6.dll>

void loadDOMsmart()
{
    MSXML2::IXMLDOMDocumentPtr pXMLDom;
    HRESULT hr= pXMLDom.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER);
    if (FAILED(hr)) 
   {
      printf("Failed to instantiate an XML DOM.\n");
      return;
   }

    try
    {
        pXMLDom->async = VARIANT_FALSE;
        pXMLDom->validateOnParse = VARIANT_FALSE;
        pXMLDom->resolveExternals = VARIANT_FALSE;

        if(pXMLDom->load("stocks.xml") == VARIANT_TRUE)
        {
            printf("XML DOM loaded from stocks.xml:\n%s\n", (LPCSTR)pXMLDom->xml);
        }
        else
        {
            // Failed to load xml
            printf("Failed to load DOM from stocks.xml. %s\n",
                   (LPCSTR)pXMLDom->parseError->Getreason());
        }
    }
    catch(_com_error errorObject)
    {
        printf("Exception thrown, HRESULT: 0x%08x", errorObject.Error());
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = CoInitialize(NULL);
    if(SUCCEEDED(hr))
    {
        loadDOMsmart();
        CoUninitialize();
    }

    return 0;

}

To add the loadDOMsmart source code 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 loadDOMsmart.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.