I'm trying to validate some XML against an XSD using C++.
I found an example for doing this in the XML SDK, following that example I came up with this code:
_bstr_t Validate(LPSTR sXML, LPSTR sXSD)
{
_bstr_t sReturn;
BSTR bstrSchemaName = (BSTR)"x-schema:XQL";
MSXML2::IXMLDOMDocument2 *pXSDDoc;
MSXML2::IXMLDOMDocument2 *pTestDoc;
MSXML2::IXMLDOMSchemaCollection *pXMLSchema;
HRESULT hr;
try
{
hr = CoInitialize(NULL);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pTestDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr=pTestDoc->put_async(VARIANT_FALSE);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_SERVER,
IID_IXMLDOMSchemaCollection, (void**)&pXMLSchema);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXSDDoc->loadXML((BSTR)sXSD);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
VARIANT varValue;
varValue.vt = VT_DISPATCH;
varValue.pdispVal = pXMLSchema;
hr = pTestDoc->putref_schemas(varValue);
SUCCEEDED(hr) ? 0 : throw hr;
//if this succeeds loading then the XML is Valid
hr = pTestDoc->loadXML((BSTR)sXML);
if(SUCCEEDED(hr))
{
sReturn = (_bstr_t)"XML is Valid.";
}
else
{
sReturn = (_bstr_t)"Error, XML is not Valid.";
}
RELEASE(pXSDDoc);
RELEASE(pXMLSchema);
RELEASE(pTestDoc);
}
catch(...)
{
sReturn = (_bstr_t)"Error, From Catch...";
if(pXSDDoc)
RELEASE(pXSDDoc);
if(pXMLSchema)
RELEASE(pXMLSchema);
if(pXMLSchema)
RELEASE(pTestDoc);
}
return sReturn;
}
An error is generated at the line: hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
I cannot figure out why this error occurs, does anyone have any ideas?
Thank you (a lot)
Jerasi.
I found an example for doing this in the XML SDK, following that example I came up with this code:
_bstr_t Validate(LPSTR sXML, LPSTR sXSD)
{
_bstr_t sReturn;
BSTR bstrSchemaName = (BSTR)"x-schema:XQL";
MSXML2::IXMLDOMDocument2 *pXSDDoc;
MSXML2::IXMLDOMDocument2 *pTestDoc;
MSXML2::IXMLDOMSchemaCollection *pXMLSchema;
HRESULT hr;
try
{
hr = CoInitialize(NULL);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER,
IID_IXMLDOMDocument2, (void**)&pTestDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr=pTestDoc->put_async(VARIANT_FALSE);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_SERVER,
IID_IXMLDOMSchemaCollection, (void**)&pXMLSchema);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXSDDoc->loadXML((BSTR)sXSD);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
VARIANT varValue;
varValue.vt = VT_DISPATCH;
varValue.pdispVal = pXMLSchema;
hr = pTestDoc->putref_schemas(varValue);
SUCCEEDED(hr) ? 0 : throw hr;
//if this succeeds loading then the XML is Valid
hr = pTestDoc->loadXML((BSTR)sXML);
if(SUCCEEDED(hr))
{
sReturn = (_bstr_t)"XML is Valid.";
}
else
{
sReturn = (_bstr_t)"Error, XML is not Valid.";
}
RELEASE(pXSDDoc);
RELEASE(pXMLSchema);
RELEASE(pTestDoc);
}
catch(...)
{
sReturn = (_bstr_t)"Error, From Catch...";
if(pXSDDoc)
RELEASE(pXSDDoc);
if(pXMLSchema)
RELEASE(pXMLSchema);
if(pXMLSchema)
RELEASE(pTestDoc);
}
return sReturn;
}
An error is generated at the line: hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
I cannot figure out why this error occurs, does anyone have any ideas?
Thank you (a lot)
Jerasi.