Source: loadDOM.cpp
This C/C++ source code performs the following steps:
- Creates an XML DOM object (
pXMLDom
) and sets it to synchronous mode. - Calls the
load
method onpXMLDom
, specifying the path to stocks.xml.
C/C++ Source File (loadDom.cpp)
#include <stdio.h> #include <windows.h> #import <msxml5.dll> raw_interfaces_only using namespace MSXML2; // Macro that calls a COM method returning HRESULT value: #define HRCALL(a, errmsg) \ do { \ hr = (a); \ if (FAILED(hr)) { \ dprintf( "%s:%d HRCALL Failed: %s\n 0x%.8x = %s\n", \ __FILE__, __LINE__, errmsg, hr, #a ); \ goto clean; \ } \ } while (0) // Helper function that put output in stdout and debug window // in Visual Studio: void dprintf( char * format, ...) { static char buf[1024]; va_list args; va_start( args, format ); vsprintf( buf, format, args ); va_end( args); OutputDebugStringA( buf); printf("%s", buf); } // Helper function to create a DOM instance: IXMLDOMDocument * DomFromCOM() { HRESULT hr; IXMLDOMDocument *pxmldoc = NULL; HRCALL( CoCreateInstance(__uuidof(DOMDocument50), NULL, CLSCTX_INPROC_SERVER, __uuidof(IXMLDOMDocument), (void**)&pxmldoc), "Create a new DOMDocument"); HRCALL( pxmldoc->put_async(VARIANT_FALSE), "should never fail"); HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE), "should never fail"); HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE), "should never fail"); return pxmldoc; clean: if (pxmldoc) { pxmldoc->Release(); } return NULL; } int main(int argc, char* argv[]) { IXMLDOMDocument *pXMLDom=NULL; IXMLDOMParseError *pXMLErr=NULL; BSTR bstr = NULL; VARIANT_BOOL status; VARIANT var; HRESULT hr; CoInitialize(NULL); pXMLDom = DomFromCOM(); if (!pXMLDom) goto clean; VariantInit(&var); V_BSTR(&var) = SysAllocString(L"stocks.xml"); V_VT(&var) = VT_BSTR; HRCALL(pXMLDom->load(var, &status), ""); if (status!=VARIANT_TRUE) { HRCALL(pXMLDom->get_parseError(&pXMLErr),""); HRCALL(pXMLErr->get_reason(&bstr),""); dprintf("Failed to load DOM from stocks.xml. %S\n", bstr); goto clean; } HRCALL(pXMLDom->get_xml(&bstr), ""); dprintf("XML DOM loaded from stocks.xml:\n%S\n",bstr); clean: if (bstr) SysFreeString(bstr); if (&var) VariantClear(&var); if (pXMLErr) pXMLErr->Release(); if (pXMLDom) pXMLDom->Release(); CoUninitialize(); return 0; }
To add loadDOM.cpp to the project
- 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 loadDOM.cpp.
- 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.