Partager via


C-C++ Code (errorParams.cpp)

 

#include <stdio.h>
#include �msxml6.h�

int main(int argc, char* argv[])
{
    HRESULT hr;

    CoInitialize(NULL);

            
    IXMLDOMDocument3Ptr pXMLDoc;
    hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));
    if (FAILED(hr)) 
    {
        printf("Failed to CreateInstance on an XML DOM");
        return -1;
    }
    pXMLDoc->async = VARIANT_FALSE; 
    pXMLDoc->validateOnParse = VARIANT_FALSE;    
    hr = pXMLDoc->setProperty("MultipleErrorMessages", VARIANT_TRUE);
    if (FAILED(hr))
    {
        printf("Failed to enable mulitple validation errors\n");
        return -1;
    }
    if(pXMLDoc->load("books.xml")!=VARIANT_TRUE)
    {
        printf("Failed to load DOM from books.xml. %s\n",
            (LPCSTR)pXMLDoc->parseError->reason);
        return -1;
    }

    IXMLDOMDocument3Ptr pXSDDoc;
    hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument60));
    if (FAILED(hr)) 
    {
        printf("Failed to CreateInstance on an XSD Doc");
        return -1;
    }
    pXSDDoc->async = VARIANT_FALSE; 
    pXSDDoc->validateOnParse = VARIANT_FALSE;    
    if(pXSDDoc->load("books.xsd")!=VARIANT_TRUE)
    {
        printf("Failed to load XSD from books.xsd. %s\n",
            (LPCSTR)pXSDDoc->parseError->reason);
        return -1;
    }

    
    IXMLDOMSchemaCollectionPtr pSCache;
    hr = pSCache.CreateInstance(__uuidof(XMLSchemaCache60));
    if (FAILED(hr)) {
        printf("Cannot instantiate XMLSchemaCache60\n");
        return -1;
    }
    pXMLDoc->schemas = pSCache.GetInterfacePtr();
      
    hr = pSCache->add("urn:books", pXSDDoc.GetInterfacePtr());
    if (FAILED(hr)) {
        printf("Cannot add 'urn:books' to schema cache.\n");
        return -1;
    }

    printf("Validating DOM...\n");
    // Validate the entire DOM object.
    IXMLDOMParseError2Ptr pError =pXMLDoc->validate();
    if (pError->errorCode != 0) {
        printf("invalid dom:\n\treason:\n%s",
            (LPCSTR)pError->reason);
        printf("\terrorXPath:\n%s",
            (LPCSTR)pError->errorXPath);
        printf("\nParameters Count: %d\n",
            pError->errorParametersCount);

 
        for (int i=0; i<pError->errorParametersCount; i++)
        {
            printf("\terrorParameters(%d): %s\n",
                i, (LPCSTR)pError->errorParameters(i));
        }
    }
    else
        printf("\tDOM is valid:\n%s\n",
            (LPCSTR)pXMLDoc->xml);


    printf("\n\nValidating nodes...\n");
    MSXML2::IXMLDOMNodeListPtr pNodeList;
    pNodeList = pXMLDoc->selectNodes("//book");
    for (int i=0; i<pNodeList->length; i++)
    {
        pError = pXMLDoc->validateNode(pNodeList->item[i]);
        if (pError->errorCode != 0) 
        {
            printf("\nNode is invalid:\n\treason: %s",
                (LPCSTR)pError->reason);
            printf("\terrorXPath:\n%s",
                (LPCSTR)pError->errorXPath);
            printf("\nParameters Count: %d\n",
                pError->errorParametersCount);
            for (int j=0; j<pError->errorParametersCount; j++)
            {
                printf("\terrorParameters(%d): %s\n",
                    j, (LPCSTR)pError->errorParameters(j));
            }
        }
        else
        {
            MSXML2::IXMLDOMNodePtr pNode=pNodeList->item[i];
            printf("\nNode is valid:\n%s\n",
                (LPCSTR)pNode->xml);
        }

    }

    pError.Release();
    pXMLDoc.Release();
    pNodeList.Release();
    pSCache.Release();
    pXSDDoc.Release();

    CoUninitialize();
    return 0;
}

Try It!

  1. Start Visual C++.

  2. From the File menu, select New. On the Projects tab of the New dialog box that appears, select Win32 Console Application in the left pane. Then type "errorParamsProj" in the Project name field. For the project Location field, either accept the default setting or choose another location. Click OK.

  3. TheWin32 Console Application property page will appear. For the type of Console Application, select An empty project and click Finish. When the New Project Information box displays, click OK.

  4. Select FileView on the project browser, and highlight errorParamsProj files. From the File menu, select New.

  5. On the Files tab of the New dialog box, highlight C++ Source File. Then type "books.xml" in the File name text box.

  6. Click OK.

  7. Copy the XML data file (books.xml), and paste it into the text file you just created.

  8. Repeat steps 4-7 for the XSD listing (books.xsd).

    Note

    You can also copy these files into the project's main directory using Windows Explorer (or a command prompt).

  9. Repeat steps 4-7 for the C++ source code above (errorParams.cpp).

  10. Build the sample by selecting Build errorParamsProj.exe from the Build menu.

  11. Execute the sample application by selecting !Execute errorParamsProj.exe from the Build menu.

  12. Verify that your output is the same as that listed in Output for the errorParams Example.