#include <stdio.h> #import <msxml5.dll> using namespace MSXML2; int main(int argc, char* argv[]) { HRESULT hr; CoInitialize(NULL); IXMLDOMDocument3Ptr pXMLDoc; hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument50)); if (FAILED(hr)) { printf("Failed to CreateInstance on an XML DOM"); return -1; } pXMLDoc->async = VARIANT_FALSE; pXMLDoc->validateOnParse = VARIANT_FALSE; hr = pXMLDoc->setProperty("MultipleErrorMessages", VARIANT_TRUE); if (FAILED(hr)) { printf("Failed to enable mulitple validation errors\n"); return -1; } if(pXMLDoc->load("books.xml")!=VARIANT_TRUE) { printf("Failed to load DOM from books.xml. %s\n", (LPCSTR)pXMLDoc->parseError->reason); return -1; } // Validate the document as a whole, relying on the // xsi:schemalocation attribute to resolve where to // to read the schema definition. IXMLDOMParseError2Ptr pError =pXMLDoc->validate(); if (pError->errorCode != 0) { IXMLDOMParseErrorCollectionPtr pErrSet; pErrSet = pError->allErrors; IXMLDOMParseError2Ptr pErr=NULL; int i=0; while (NULL != (pErr = pErrSet->next)) { printf("errorItem[%d]: %s\n", i, (LPCSTR)pErr->reason); } } else printf("DOM is valid:\n%s\n",(LPCSTR)pXMLDoc->xml); pXMLDoc.Release(); pError.Release(); CoUninitialize(); return 0; }
Try It!
Note You can also copy these files into the project's main directory using Windows Explorer (or a command prompt).