使用MSXML 6.0向SAXXMLReader60实例添加模式集合

Adding schema collection to SAXXMLReader60 instance, using MSXML 6.0

本文关键字:实例 添加 模式 集合 SAXXMLReader60 MSXML 使用      更新时间:2023-10-16

我尝试使用MSXML 6.0编写xsd验证器实用程序。一切都很好,除了这一行:

this->myReader->putProperty(L"schemas", pXS);
error C2664: 'ISAXXMLReader::putProperty' : cannot convert parameter 2 from 'MSXML2::IXMLDOMSchemaCollectionPtr' to 'VARIANT'

这是可以理解的,但是如何将模式实例添加到阅读器中呢?

仅供参考,myReader是:

ISAXXMLReader * myReader;

,初始化如下:

HRESULT hr = CoCreateInstance( __uuidof(SAXXMLReader60), 
                               NULL, 
                               CLSCTX_ALL,
                               __uuidof(ISAXXMLReader),
                               (void **)&this->myReader);

和pXS是:

MSXML2::IXMLDOMSchemaCollectionPtr   pXS;
pXS.CreateInstance(__uuidof(MSXML2::XMLSchemaCache60), NULL, CLSCTX_INPROC_SERVER);

我读到的一些链接:

http://msdn.microsoft.com/en-us/library/ms762787 (v = VS.85) . aspx

http://support.microsoft.com/kb/309535

http://msdn.microsoft.com/en-us/library/windows/desktop/cc507429 (v = VS.85) . aspx

像往常一样MSDN文档真的很好…

他们提供的例子工作,但我需要收集所有的错误,所以我翻译了vb的例子在一个链接到c++。唯一的错误是这样的。如有任何帮助,不胜感激。

编辑:

点击链接后:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms766451 (v = vs.85) . aspx

c++ section:

要与指定模式关联的命名空间。空字符串"将把模式与空命名空间xmlns="相关联。

然后这样做:

nResult = pIXMLDOMSchemaCollection2Ptr->add(_T(""), _T("c:\temp\collection.xsd"));

一个很好的崩溃发生了。有人知道如何在这个东西中添加一个没有命名空间的模式吗?

通过搜索我找到了问题的解决方案:

            MSXML2::IXMLDOMSchemaCollectionPtr pSchemaCache;
            pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache60));
            if(FAILED(pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache60))))
            {
                throw AX::Base::Exception(__FILE__, __FUNCTION__, __LINE__, AX::Base::Exception::ABORT_EXECUTION, "Could not create Schema cache instance!");
            }
            if(FAILED(pSchemaCache->add(L"urn:books", myXSDFile.c_str())))
            {
                throw AX::Base::Exception(__FILE__, __FUNCTION__, __LINE__, AX::Base::Exception::INVALID_PARAM_ERROR, "Could not add schema %s to Schema cache instance!", this->myXSDFile.c_str());
            }
            this->myReader->putProperty(L"schemas", _variant_t(pSchemaCache.GetInterfacePtr()));

希望其他人能从中受益。

编辑:

如何添加空命名空间:

    bool const XmlValidator::validate()
    {
        //clear any previous validation errors
        this->myErrorHandler->clearErrorsAndWarnings();
        //do validation
        const std::wstring ns = this->converStringToWString(this->myXMLNamespace);
        try
        {
            // Check for existing schema associated with this namespace URI.
            ISchema* pExistingSchema = NULL;
            _bstr_t bstrNamespace = ns.c_str();
            HRESULT hr = this->mySchemaCache->getSchema(bstrNamespace, &pExistingSchema);
            if ( SUCCEEDED(hr) )
            {
                // Remove the existing schema.
                hr = this->mySchemaCache->remove(bstrNamespace);
                if ( FAILED(hr) )
                    return false;
            }
            // Add the new schema.
            hr = this->mySchemaCache->add(bstrNamespace, _variant_t(this->converStringToWString(this->myXSDFile).c_str()));
            if ( FAILED(hr) )
                return false;
        }
        catch(...)
        {
            throw AX::Base::Exception(__FILE__, __FUNCTION__, __LINE__, AX::Base::Exception::INVALID_PARAM_ERROR, "Xml namespace %s is different from Xsd namespace!", this->myXMLNamespace.c_str());
        }
        this->myReader->parseURL(this->converStringToWString(this->myXMLFile).c_str());
        //return true if there were no validation errors at all
        return this->myErrorHandler->getMyErrorsAndWarnings().size() == 0;  
    }

注意这一行: _bstr_t bstrNamespace = ns.c_str();