zoukankan      html  css  js  c++  java
  • XML实例入门2

    工具:notepad++、VS2008(MSXML6.0)

    来自msdn的例子(经过修改,因为升级到MSXML6.0,有些关键字不太一样了),

    需要文件books.xmlbooks.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
    ");
     22         return -1;
     23     }
     24     if(pXMLDoc->load("..\books.xml")!=VARIANT_TRUE){
     25         printf("Failed to load DOM from books.xml. %s
    ",
     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
    ",
     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
    ");
     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.
    ");
     56         return -1;
     57     }
     58 
     59     printf("Validating DOM...
    ");
     60     // Validate the entire DOM object.
     61     IXMLDOMParseError2Ptr pError =pXMLDoc->validate();
     62     if (pError->errorCode != 0) {
     63         printf("invalid dom:
    	reason:
    %s",
     64             (LPCSTR)pError->reason);
     65         printf("	errorXPath:
    %s",
     66             (LPCSTR)pError->errorXPath);
     67         printf("
    Parameters Count: %d
    ",
     68             pError->errorParametersCount);
     69  
     70         for (int i=0; i<pError->errorParametersCount; i++){
     71             printf("	errorParameters(%d): %s
    ",
     72                 i, (LPCSTR)pError->errorParameters(i));
     73         }
     74      }else{
     75         printf("	DOM is valid:
    %s
    ",
     76             (LPCSTR)pXMLDoc->xml);
     77     }
     78 
     79     printf("
    
    Validating nodes...
    ");
     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("
    Node %d is invalid:
    	reason: %s", i,
     86                 (LPCSTR)pError->reason);
     87             printf("	errorXPath:
    %s",
     88                 (LPCSTR)pError->errorXPath);
     89             printf("
    Parameters Count: %d
    ",
     90                 pError->errorParametersCount);
     91             for (int j=0; j<pError->errorParametersCount; j++){
     92                 printf("	errorParameters(%d): %s
    ",
     93                     j, (LPCSTR)pError->errorParameters(j));
     94             }
     95         }else{
     96             MSXML2::IXMLDOMNodePtr pNode=pNodeList->item[i];
     97             printf("
    Node %d is valid:
    %s
    ", 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

     

  • 相关阅读:
    Android -- 经验分享
    Android -- 获取汉字的首字母
    Android -- PowerManager和PowerManager.WakeLock
    内存堆和栈的区别
    Java BigDecimal大数字操作
    myqsl for C# 驱动包下载地址-官网
    ASP.NET 查询客户端请求IP地址
    Chapter 6 — Improving ASP.NET Performance
    WebSocket 支持的浏览器
    Local System、Local Service與Network Service
  • 原文地址:https://www.cnblogs.com/quantumplan/p/3593761.html
Copyright © 2011-2022 走看看