Xtreem
asked on
How do I validate XML document against an XML schema and get ALL parsing errors returned?
I am using XML DOM to validate an XML document against an XML schema. However, it only returns the first parsing error found. The XML is well-formed so I'm not interested in these kind of errors, only errors where the data doesn't conform to the schema provided. Is there any way (using XML DOM or otherwise) to have it return all errors within the document, or provide a means of iterating through them - without correcting each error in between, of course? To put this into perspective, this is the code I currently have.
IXMLDOMSchemaCollectionPtr pXS;
IXMLDOMDocument2Ptr pXD;
// Create a schema cache and add the XML schema to it
HRESULT hr = pXS.CreateInstance(__uuido f(XMLSchem aCache60)) ;
hr = pXS->add(_T(""), _T("C:\\MySchema.xsd");
// Create a DOMDocument and set its properties
hr = pXD.CreateInstance(__uuido f(DOMDocum ent60));
pXD->async = VARIANT_FALSE;
pXD->validateOnParse = VARIANT_TRUE;
pXD->resolveExternals = VARIANT_TRUE;
// Assign the schema cache to the DOMDocument's schemas collection
pXD->schemas = pXS.GetInterfacePtr();
// Load the XML document data file as the DOM document
hr = pXD->load(_T("C:\\MyData.x ml"));
MSXML2::IXMLDOMParseErrorP tr pErr = pXD->parseError;
// Return validation results in message to the user
if (FAILED(pErr->errorCode))
{
_tprintf(_T("\nValidation failed\n=================\ n")
_T("Reason: %s\nSource: %s\nLine: %i\n"),
static_cast<LPTSTR>(pErr-> Getreason( )),
static_cast<LPTSTR>(pErr-> GetsrcText ()),
pErr->Getline());
}
else
{
_tprintf(_T("\nValidation succeeded\n=============== =====\n%s" ),
static_cast<LPTSTR>(pXD->x ml));
}
Many thanks,
Jason
IXMLDOMSchemaCollectionPtr
IXMLDOMDocument2Ptr pXD;
// Create a schema cache and add the XML schema to it
HRESULT hr = pXS.CreateInstance(__uuido
hr = pXS->add(_T(""), _T("C:\\MySchema.xsd");
// Create a DOMDocument and set its properties
hr = pXD.CreateInstance(__uuido
pXD->async = VARIANT_FALSE;
pXD->validateOnParse = VARIANT_TRUE;
pXD->resolveExternals = VARIANT_TRUE;
// Assign the schema cache to the DOMDocument's schemas collection
pXD->schemas = pXS.GetInterfacePtr();
// Load the XML document data file as the DOM document
hr = pXD->load(_T("C:\\MyData.x
MSXML2::IXMLDOMParseErrorP
// Return validation results in message to the user
if (FAILED(pErr->errorCode))
{
_tprintf(_T("\nValidation failed\n=================\
_T("Reason: %s\nSource: %s\nLine: %i\n"),
static_cast<LPTSTR>(pErr->
static_cast<LPTSTR>(pErr->
pErr->Getline());
}
else
{
_tprintf(_T("\nValidation succeeded\n===============
static_cast<LPTSTR>(pXD->x
}
Many thanks,
Jason
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.thescripts.com/forum/thread604755.html