XML实例入门2
工具:notepad++、VS2008(MSXML6.0)
来自msdn的例子(经过修改,因为升级到MSXML6.0,有些关键字不太一样了),
需要文件books.xml,books.vsd(博客只支持xml文件,vsd修改后缀就可以)
1 #include <stdio.h> 2 #import <msxml6.dll> 3 using namespace MSXML2; 4 5 int main(int argc, char* argv[]) 6 { 7 HRESULT hr; 8 9 CoInitialize(NULL); 10 11 IXMLDOMDocument3Ptr pXMLDoc; 12 hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60)); 13 if (FAILED(hr)) { 14 printf("Failed to CreateInstance on an XML DOM"); 15 return -1; 16 } 17 pXMLDoc->async = VARIANT_FALSE; 18 pXMLDoc->validateOnParse = VARIANT_FALSE; 19 hr = pXMLDoc->setProperty("MultipleErrorMessages", VARIANT_TRUE); 20 if (FAILED(hr)){ 21 printf("Failed to enable mulitple validation errors\n"); 22 return -1; 23 } 24 if(pXMLDoc->load("..\\books.xml")!=VARIANT_TRUE){ 25 printf("Failed to load DOM from books.xml. %s\n", 26 (LPCSTR)pXMLDoc->parseError->reason); 27 return -1; 28 } 29 30 IXMLDOMDocument3Ptr pXSDDoc; 31 hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument60)); 32 if (FAILED(hr)) { 33 printf("Failed to CreateInstance on an XSD Doc"); 34 return -1; 35 } 36 pXSDDoc->async = VARIANT_FALSE; 37 pXSDDoc->validateOnParse = VARIANT_FALSE; 38 if(pXSDDoc->load("..\\books.xsd")!=VARIANT_TRUE){ 39 printf("Failed to load XSD from books.xsd. %s\n", 40 (LPCSTR)pXSDDoc->parseError->reason); 41 return -1; 42 } 43 44 45 IXMLDOMSchemaCollectionPtr pSCache; 46 hr = pSCache.CreateInstance(__uuidof(XMLSchemaCache60)); 47 if (FAILED(hr)) { 48 printf("Cannot instantiate XMLSchemaCache50\n"); 49 return -1; 50 } 51 pXMLDoc->schemas = pSCache.GetInterfacePtr(); 52 53 hr = pSCache->add("urn:books", pXSDDoc.GetInterfacePtr()); 54 if (FAILED(hr)) { 55 printf("Cannot add 'urn:books' to schema cache.\n"); 56 return -1; 57 } 58 59 printf("Validating DOM...\n"); 60 // Validate the entire DOM object. 61 IXMLDOMParseError2Ptr pError =pXMLDoc->validate(); 62 if (pError->errorCode != 0) { 63 printf("invalid dom:\n\treason:\n%s", 64 (LPCSTR)pError->reason); 65 printf("\terrorXPath:\n%s", 66 (LPCSTR)pError->errorXPath); 67 printf("\nParameters Count: %d\n", 68 pError->errorParametersCount); 69 70 for (int i=0; i<pError->errorParametersCount; i++){ 71 printf("\terrorParameters(%d): %s\n", 72 i, (LPCSTR)pError->errorParameters(i)); 73 } 74 }else{ 75 printf("\tDOM is valid:\n%s\n", 76 (LPCSTR)pXMLDoc->xml); 77 } 78 79 printf("\n\nValidating nodes...\n"); 80 MSXML2::IXMLDOMNodeListPtr pNodeList; 81 pNodeList = pXMLDoc->selectNodes("//book"); 82 for (int i=0; i<pNodeList->length; i++){ 83 pError = pXMLDoc->validateNode(pNodeList->item[i]); 84 if (pError->errorCode != 0) { 85 printf("\nNode %d is invalid:\n\treason: %s", i, 86 (LPCSTR)pError->reason); 87 printf("\terrorXPath:\n%s", 88 (LPCSTR)pError->errorXPath); 89 printf("\nParameters Count: %d\n", 90 pError->errorParametersCount); 91 for (int j=0; j<pError->errorParametersCount; j++){ 92 printf("\terrorParameters(%d): %s\n", 93 j, (LPCSTR)pError->errorParameters(j)); 94 } 95 }else{ 96 MSXML2::IXMLDOMNodePtr pNode=pNodeList->item[i]; 97 printf("\nNode %d is valid:\n%s\n", i, 98 (LPCSTR)pNode->xml); 99 } 100 } 101 102 pError.Release(); 103 pXMLDoc.Release(); 104 pNodeList.Release(); 105 pSCache.Release(); 106 pXSDDoc.Release(); 107 108 CoUninitialize(); 109 return 0; 110 }
注:MSXML2::IXMLDOMNodeListPtr与MSXML2::IXMLDOMNodePtr,命名空间重复,显示指明命名空间即可
其他参考:XML实例入门1