troubleshooting Question

How do I validate XML document against an XML schema and get ALL parsing errors returned?

Avatar of Xtreem
XtreemFlag for United Kingdom of Great Britain and Northern Ireland asked on
C++XMLSystem Programming
2 Comments1 Solution1000 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros