XML : Load xml file.

//#include <Windows.h> #include <tchar.h> #include <ObjBase.h> #include <MsXml2.h> #include <stdio.h> void dprintf(char* format,...) { static char buf[1024]; va_list args; va_start(args,format); vsprintf_s(buf,format,args); va_end(args); OutputDebugStringA(buf); printf("%s",buf); } #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); IXMLDOMDocument * DomFromCom() { HRESULT hr; IXMLDOMDocument *pXMLDom = NULL; HRCALL(CoCreateInstance(__uuidof(DOMDocument60), NULL, CLSCTX_INPROC_SERVER, __uuidof(IXMLDOMDocument), (void**)&pXMLDom), L"Create a new document."); HRCALL(pXMLDom->put_async(VARIANT_FALSE),L"should never fail."); HRCALL(pXMLDom->put_validateOnParse(VARIANT_FALSE),L"shoule never fail."); HRCALL(pXMLDom->put_resolveExternals(VARIANT_FALSE),L"should never fail."); return pXMLDom; clean: if(pXMLDom) pXMLDom->Release(); return NULL; } void _tmain(int argc,_TCHAR* argv[]) { CoInitialize(NULL); HRESULT hr; IXMLDOMDocument *pXMLDom = NULL; pXMLDom = DomFromCom(); if(!pXMLDom) goto clean; VARIANT var; VariantInit(&var); V_BSTR(&var) = SysAllocString(L"stocks.xml"); V_VT(&var) = VT_BSTR; VARIANT_BOOL status; HRCALL(pXMLDom->load(var,&status),L""); IXMLDOMParseError *pXMLErr = NULL; BSTR bstr; if(VARIANT_TRUE != status) { HRCALL(pXMLDom->get_parseError(&pXMLErr),L""); HRCALL(pXMLErr->get_reason(&bstr),L""); dprintf("Failed to load DOM from stocks.xml. %S/n", bstr); goto clean; } HRCALL(pXMLDom->get_xml(&bstr),""); dprintf("XML load DOM from stocks.xml. %S/n",bstr); clean: if(bstr) SysFreeString(bstr); if(&var) VariantClear(&var); if(pXMLErr) pXMLErr->Release(); if(pXMLDom) pXMLDom->Release(); CoUninitialize(); }

 

 

stocks.xml

<?xml version="1.0"?> <?xml:stylesheet type="text/xsl" href="stock.xsl"?> <portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes"> <stock exchange="nasdaq"> <name>new</name> <symbol>zzzz</symbol> <price dt:dt="number">20.313</price> </stock> <stock exchange="nyse"> <name>zacx corp</name> <symbol>ZCXM</symbol> <price dt:dt="number">28.875</price> </stock> <stock exchange="nasdaq"> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price dt:dt="number">92.250</price> </stock> <stock exchange="nasdaq"> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price dt:dt="number">20.313</price> </stock> </portfolio>

你可能感兴趣的:(XML : Load xml file.)