I'm would like to validate XML in a function, I'm working with C++ and so far got this following code:
_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_INPROC_SERVER,
IID_IXMLDOMDocument, (void**)&pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
IID_IXMLDOMDocument, (void**)&pTestDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_INPROC_SERVER,
IID_IXMLDOMParseError, (void**)&pXMLSchema);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXSDDoc->loadXML((BSTR)sXSD);
SUCCEEDED(hr) ? 0 : throw hr;
//at this point an error is generated and jumping to catch
hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pTestDoc->loadXML((BSTR)sXML);
if(SUCCEEDED(hr))
{
sReturn = (_bstr_t)"XML is valid.";
}
else
{
sReturn = (_bstr_t)"XML is not valid.";
}
RELEASE(pXSDDoc);
RELEASE(pXMLSchema);
RELEASE(pTestDoc);
}
catch(...)
{
sReturn = (_bstr_t)"Error, jumped to Catch";
if(pXSDDoc)
RELEASE(pXSDDoc);
if(pXMLSchema)
RELEASE(pXMLSchema);
if(pXMLSchema)
RELEASE(pTestDoc);
}
return sReturn;
_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_INPROC_SERVER,
IID_IXMLDOMDocument, (void**)&pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
IID_IXMLDOMDocument, (void**)&pTestDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_INPROC_SERVER,
IID_IXMLDOMParseError, (void**)&pXMLSchema);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pXSDDoc->loadXML((BSTR)sXSD);
SUCCEEDED(hr) ? 0 : throw hr;
//at this point an error is generated and jumping to catch
hr = pXMLSchema->add(bstrSchemaName, pXSDDoc);
SUCCEEDED(hr) ? 0 : throw hr;
hr = pTestDoc->loadXML((BSTR)sXML);
if(SUCCEEDED(hr))
{
sReturn = (_bstr_t)"XML is valid.";
}
else
{
sReturn = (_bstr_t)"XML is not valid.";
}
RELEASE(pXSDDoc);
RELEASE(pXMLSchema);
RELEASE(pTestDoc);
}
catch(...)
{
sReturn = (_bstr_t)"Error, jumped to Catch";
if(pXSDDoc)
RELEASE(pXSDDoc);
if(pXMLSchema)
RELEASE(pXMLSchema);
if(pXMLSchema)
RELEASE(pTestDoc);
}
return sReturn;