Link to home
Start Free TrialLog in
Avatar of Xtreem
XtreemFlag for United Kingdom of Great Britain and Northern Ireland

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(__uuidof(XMLSchemaCache60));
    hr = pXS->add(_T(""), _T("C:\\MySchema.xsd");

    // Create a DOMDocument and set its properties
    hr = pXD.CreateInstance(__uuidof(DOMDocument60));
    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.xml"));

    MSXML2::IXMLDOMParseErrorPtr 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->xml));
    }


Many thanks,
Jason
ASKER CERTIFIED SOLUTION
Avatar of kawas
kawas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This thread outlines various IDEs, etc that can 'find all errors' at once

http://www.thescripts.com/forum/thread604755.html