c++ - 使用 msxml 解析器验证 xml 模式

标签 c++ xsd msxml

我想根据 XML 架构文件验证 XML 文件。它是一个简单的 xml 文件,不包括命名空间等。我想在 c++ 中使用 MSXML 6.0 执行此操作。

最佳答案

您可以在加载时进行验证。这是来自 Windows/MSXML SDK 的示例代码:

   IXMLDOMSchemaCollectionPtr   pXS;
   IXMLDOMDocument2Ptr          pXD = NULL;
   IXMLDOMParseErrorPtr         pErr = NULL;
   _bstr_t                      strResult = "";

   HRESULT hr = pXS.CreateInstance(__uuidof(XMLSchemaCache50));
   hr = pXS->add("urn:namespace", "myschema.xsd");

   // Create a DOMDocument and set its properties.
   hr = pXD.CreateInstance(__uuidof(DOMDocument50));

   // Assign the schema cache to the DOMDocument's
   // schemas collection.
   pXD->schemas = pXS.GetInterfacePtr();

   // Load books.xml as the DOM document.
   pXD->async = VARIANT_FALSE;
   pXD->validateOnParse = VARIANT_TRUE;
   pXD->resolveExternals = VARIANT_TRUE;
   hr = pXD->load("TheXmlDocument.xml");

   // check hr and pXD->errorCode here

您可以 download the MSXML6 SDK获取此示例和许多其他示例。注意:它不会安装在 Vista 上。如果您运行 Vista,则获取 Windows SDK .

关于c++ - 使用 msxml 解析器验证 xml 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1048764/

相关文章:

c++ - Visual C++ 中的容错 DLL 使用

c++ - 访问 union 的成员变量 : declared for uint64, 但想要 uint32 中的内容。 C++

xml - MSXML 总是向元素添加命名空间

c++ - 无法使用 MSXML6 加载带有 UTF-8 BOM 标记的 XML 文件

java - 如何判断 XML 文档是否针对 DTD 或 XSD 进行验证?

rss - 在哪里可以找到 RSS 2.0 的官方 XSD 架构

java - 如何将 XSD 类型导入根模式?

c++ - Qt Creator 项目在构建之间删除 Makefile

c++ - 通过 static_assert 强制执行模板类型

c++ - 将 std::string 数字转换为十六进制数