• XML实例入门1


          虽然网上会有XML入门或者多少天教会XML,一般都是从普通的功能概念开始啰嗦,个人还是比较喜欢从实例开始,遇到不懂直接查(不过这次选了一个比较难得例子,研究了好久^_^)。刚开始看了一个vc6.0XML的例子,调通了,不过要用VS2008开发,想移植到VS2008,挺麻烦的,就没咋看,有需要可以下载看看。

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

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

    需要文件books.xmlbooks.vsd(博客只支持xml文件,vsd修改后缀就可以)

    #include <stdio.h>
    #import <msxml6.dll>
    using namespace MSXML2;
    
    int main(int argc, char* argv[])
    {
        HRESULT hr;
    
        CoInitialize(NULL);
    
        IXMLDOMDocument3Ptr pXMLDoc;
        IXMLDOMDocument3Ptr pXSDDoc;
            IXMLDOMSchemaCollectionPtr pSCache;
        IXMLDOMParseError2Ptr pEitem;
        IXMLDOMParseError2Ptr pError;
    
            hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));
            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 multiple validation errors
    ");
                return -1;
            }
            if(pXMLDoc->load("..\books.xml")!=VARIANT_TRUE){
                printf("Failed to load Dom from books.xml. %s
    ",
                    (LPCSTR)pXMLDoc->parseError->reason);
                return -1;
            }
    
            hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument60));
            if(FAILED(hr)){
                printf("Failed to CreateInstance on an XSD DOC");
                return -1;
            }
            pXSDDoc->async = VARIANT_FALSE;
            pXSDDoc->validateOnParse = VARIANT_FALSE;
            if(pXSDDoc->load("..\books.xsd")!=VARIANT_TRUE){
                printf("Failed to load XSD from books.xsd. %s
    ",
                    (LPCSTR)pXSDDoc->parseError->reason);
                return -1;
            }
    
            hr = pSCache.CreateInstance(__uuidof(XMLSchemaCache60));
            if(FAILED(hr)){
                printf("Cannot instantiate XMLSchemaCache60
    ");
                return -1;
            }
            pXMLDoc->schemas = pSCache.GetInterfacePtr();
    
            hr = pSCache->add("urn:books", pXSDDoc.GetInterfacePtr());
            if(FAILED(hr)){
                printf("Cannot add 'urn:books' to schema cache.
    ");
                return -1;
            }
    
            //Validate the entire DOM object.
            pError = pXMLDoc->validate();
            if(pError->errorCode != 0){
                printf("Error as returned from validate():
    
    ");
                printf("	Error Code: %d
    ", pError->errorCode);
                printf("	Error reson: 
    %s", (LPCSTR)pError->reason);
                printf("	Error location: 
    %s
    ", (LPCSTR)pError->errorXPath);
                printf("	Errors count: %d
    ", pError->allErrors->length);
                printf("
    Error items from the allErrors collection:
    ");
    
                for(int i=0;i<pError->allErrors->length;i++){
                    pEitem = pError->allErrors->item[i];
                    printf("
    ErrorItem[%d]:
    ", i);
                    printf("	reason:
    %s", (LPCSTR)pEitem->reason);
                    printf("	location: 
    %s
    ", (LPCSTR)pEitem->errorXPath);
                }
            }else{
                printf("valid dom as follows:
    %s
    ",
                    (LPCSTR)pXMLDoc->xml);
            }
    
            pError.Release();
            pEitem.Release();
            pXMLDoc.Release();
            pXSDDoc.Release();
            pSCache.Release();
    
            CoUninitialize();
            return 0;
    }

     

    注:1、pXMLDoc->load("..\books.xml"),文件路径是工程根目录

     2、例子是XSD对XML文件的validate

    3、把不懂的地方查懂XML入门是没问题

  • 相关阅读:
    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun
    查看公钥
    Flutter 环境配置,创建工程
    Flutter 简介
    Mac版本 FinalShell SSH工具
    windows下如何生成公钥和私钥
    pyqt 打包为dmg文件
    apple 升级后shell切换为zsh
    dart 类共享变量
    python 获取一小时前的时间戳
  • 原文地址:https://www.cnblogs.com/quantumplan/p/3593664.html
Copyright © 2020-2023  润新知