3
我想根據XML模式文件驗證XML文件。它是一個簡單的xml文件,不包括命名空間等。我想用C++,使用MSXML 6.0。使用msxml分析器驗證xml模式
我想根據XML模式文件驗證XML文件。它是一個簡單的xml文件,不包括命名空間等。我想用C++,使用MSXML 6.0。使用msxml分析器驗證xml模式
您可以在加載時進行驗證。這是從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。