Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Validation

Status
Not open for further replies.

jerasi

Programmer
Jun 19, 2000
141
CA
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;
 
oops pressed the wrong button :)

This code doesnt seem to work (error in pXMLSchema->add...) is there a better way of doing this?
P.S. I'm using msxml 4.0

Thanks.
 
Hi Jerasi,

It look like you're getting an error on the loadxml function. The msxml parser has a built in error object called parseerror.

I don't know how to call this in C++, but you might want to log or display the properties: reason and linepos of the parseerror, so you can see the reason for the error and get the exact position in the xml where it went wrong.

Good luck!

Jordi Reineman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top