Hello, sergey22!
s>>> подскажите, как можно программно проверить соответствие
s>>> готового xml документа схеме?
s> C++
Ну не чистый C++. Используя COM.
#include <stdio.h>
#import <msxml4.dll>
using namespace MSXML2;
int checkParseError(IXMLDOMParseErrorPtr pError);
void dump_com_error(_com_error &e);
int main()
{
CoInitialize(NULL);
try{
IXMLDOMParseErrorPtr pError;
// load the XML file
// ****** you need to use IXMLDOMDocument2 interface *********
IXMLDOMDocument2Ptr pXMLDoc;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument40));
pXMLDoc->async = VARIANT_FALSE;
hr = pXMLDoc->load("c:\\book.xml");
//check on the parser error
if(hr!=VARIANT_TRUE)
{
return checkParseError(pXMLDoc->parseError);
}
//load the XSD file
IXMLDOMDocumentPtr pXSDDoc;
hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument40));
pXSDDoc->async = VARIANT_FALSE;
hr = pXSDDoc->load("books.xsd");
//check on the parser error
if(hr!=VARIANT_TRUE)
{
return checkParseError(pXSDDoc->parseError);
}
//create schemacache
IXMLDOMSchemaCollectionPtr pSchemaCache;
hr = pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache40));
pXMLDoc->schemas = pSchemaCache.GetInterfacePtr();
//hook it up with XML Document
hr = pSchemaCache->add("urn:books", pXSDDoc.GetInterfacePtr());
//call validate
pError = pXMLDoc->validate();
if(pError->errorCode != S_OK)
{
_bstr_t parseError = _bstr_t("Error code: ")+ _bstr_t(pError->errorCode) +_bstr_t("\n") + _bstr_t("Reason: ")+ pError->Getreason();
MessageBox(NULL, (char*)parseError, "Parse Error",MB_OK);
return -1;
}
else
MessageBox(NULL,"Valiation succeeded", "Results",MB_OK);
}
catch(_com_error &e)
{
dump_com_error(e);
}
::CoUninitialize();
return 0;
}
int checkParseError(IXMLDOMParseErrorPtr pError)
{
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return -1;
}
void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
Posted via RSDN NNTP Server 1.5 beta