使用MSXML 6.0向SAXXMLReader60实例添加模式集合
Adding schema collection to SAXXMLReader60 instance, using MSXML 6.0
我尝试使用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/309535http://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();
相关文章:
- 在C++中将类实例添加到对象层次结构中的问题
- 为什么自删除的全局 Vulkan 实例仅在添加层时才导致段错误?
- 将内存文件添加到 clang 编译器实例
- 我可以将一个套接字添加到多个 epoll 实例吗?
- 添加无模板实例化的访问者方法
- 对成员变量的引用在向同一向量中添加另一个实例后断开
- 将类的每个实例添加到列表对象的构造函数
- 创建 QQuickItem 子类的实例是否有问题,我不打算渲染或添加到 QML 树中的实例?
- 将类的实例传递给另一个构造函数,该构造函数将其对象添加到所传递实例所拥有的列表中
- C 将条目添加到具有字符串和类实例的STL映射中,作为指针
- 将类C++实例转换为 NSObject,然后添加到 NSMutableArray
- 将从接口派生的类的实例添加到接口指针数组中
- 一个非内联非虚拟方法向类的实例添加了多少字节?C++
- 为什么我的课'当我向向量添加实例时,会调用s析构函数
- 在 C++ 中将"this"添加到类实例化的向量
- 将解码实例添加到可以调用主类的类中
- 使用MSXML 6.0向SAXXMLReader60实例添加模式集合
- 优雅地重用将const添加到类实例化中的代码
- 如何将预先确定的模板实例化集添加到函数模板中
- 如何在不'new'的情况下将新的对象实例添加到 std::list