#include <stdio.h> #import <msxml5.dll> using namespace MSXML2; void dump_com_error(_com_error &e); void show_parse_error(_bstr_t ctx, IXMLDOMParseErrorPtr pErr); int main(int argc, char* argv[]) { IXMLDOMDocument3Ptr pXMLDoc = NULL; IXMLDOMDocument3Ptr pXSDDoc = NULL; IXMLDOMSchemaCollectionPtr pSCache = NULL; IXMLDOMNodePtr pNode = NULL; IXMLDOMNodeListPtr pNodelist= NULL; IXMLDOMParseErrorPtr pError = NULL; HRESULT hr; long i = 0; long x = 0; CoInitialize(NULL); try{ // Load validateNode.xml into a DOM instance. hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument50)); if (FAILED(hr)) { MessageBox(NULL, "Cannot create DOMDocument50 xml", "CreateInstance", MB_OK); return 0; } pXMLDoc->async = VARIANT_FALSE; pXMLDoc->validateOnParse = VARIANT_FALSE; if(pXMLDoc->load("validateNode.xml") != VARIANT_TRUE) { show_parse_error(_bstr_t("Can't load validateNode.xml\n"), pXMLDoc->parseError); return 0; } // Load validateNode.xsd into a DOM instance. hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument50)); if (FAILED(hr)) { MessageBox(NULL, "Cannot create DOMDocument50 for xsd", "CreateInstance", MB_OK); return 0; } pXSDDoc->async = VARIANT_FALSE; pXSDDoc->validateOnParse = VARIANT_FALSE; if(pXSDDoc->load("validateNode.xsd") != VARIANT_TRUE) { show_parse_error(_bstr_t("Can't load validateNode.xsd\n"), pXSDDoc->parseError); return 0; } // Create a schema cache. hr = pSCache.CreateInstance(__uuidof(XMLSchemaCache50)); if (FAILED(hr)) { MessageBox(NULL, "Cannot create XMLSchemaCache50", "CreateInstance", MB_OK); return 0; } // Point the XML doc's schema to the loaded schema cache. pXMLDoc->schemas = pSCache.GetInterfacePtr(); hr = pSCache->add("urn:books", pXSDDoc.GetInterfacePtr()); if (FAILED(hr)) { MessageBox(NULL, "Cannot add schema", "add", MB_OK); return 0; } // Validate the entire DOM object. pError =pXMLDoc->validate(); if (pError->errorCode != 0) { show_parse_error(_bstr_t("invalid dom:\n"), pError); } else MessageBox(NULL,pXMLDoc->xml, "import",MB_OK); // Validate all book nodes, node by node. pNodelist = pXMLDoc->selectNodes("//book"); x = pNodelist->length; for(i = 1; i <= x; i++) { pNode = pNodelist->nextNode(); pError = pXMLDoc->validateNode(pNode); if (pError->errorCode != 0) { show_parse_error(_bstr_t("invalid node:\n"), pError); } else MessageBox(NULL,pNode->xml, "validateNode",MB_OK); } } catch(_com_error &e) { dump_com_error(e); } pXMLDoc.Release(); pXSDDoc.Release(); pSCache.Release(); pNode.Release(); pNodelist.Release(); pError.Release(); CoUninitialize(); return 0; } void dump_com_error(_com_error &e) { printf("Error\n"); printf("\a\tCode = %08lx\n", e.Error()); printf("\a\tCode meaning = %s", e.ErrorMessage()); _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); printf("\a\tSource = %s\n", (LPCSTR) bstrSource); printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription); } void show_parse_error(_bstr_t ctx, IXMLDOMParseErrorPtr pError) { _bstr_t parseError = ctx + _bstr_t(pError->Getreason()); MessageBox(NULL,parseError, "Parse Error",MB_OK); }
Try It!
Note You can also copy the file into the project's main directory using Windows Explorer (or a command prompt).