Share via


C-C++ Code (schemaCache.cpp)

 

The following is the C/C++ code.

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll>

// Macro that calls a COM method returning HRESULT value.
#define CHK_HR(stmt)        do { hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)

void dump_com_error(_com_error &e)
{
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());

    printf("Error\n");
    printf("\a\tCode = %08lx\n", e.Error());
    printf("\a\tCode meaning = %s", e.ErrorMessage());
    printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
    printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}

_bstr_t validateFile(_bstr_t bstrFile)
{
    // Declare and initialize variables
    MSXML2::IXMLDOMSchemaCollectionPtr   pXS;
    MSXML2::IXMLDOMDocument2Ptr          pXD;
    MSXML2::IXMLDOMParseErrorPtr         pErr;
    _bstr_t bstrResult = L"";
    HRESULT hr = S_OK;

    // Create a schema cache and add books.xsd to it.
    CHK_HR(pXS.CreateInstance(__uuidof(MSXML2::XMLSchemaCache60), NULL, CLSCTX_INPROC_SERVER));
    CHK_HR(pXS->add(L"urn:books", L"sc.xsd"));

    // Create a DOMDocument and set its properties.
    CHK_HR(pXD.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER));

    // Assign the schema cache to the DOMDocument's schemas collection.
    pXD->schemas = pXS.GetInterfacePtr();

    // Load bstrFile into the DOM document.
    pXD->async = VARIANT_FALSE;
    pXD->validateOnParse = VARIANT_TRUE;
    pXD->resolveExternals = VARIANT_TRUE;

    if(pXD->load(bstrFile) != VARIANT_TRUE)
    {
        pErr = pXD->parseError;

        bstrResult = _bstr_t(L"Validation failed on ") + bstrFile +
            _bstr_t(L"\n=====================") +
            _bstr_t(L"\nReason: ") + _bstr_t(pErr->Getreason()) +
            _bstr_t(L"\nSource: ") + _bstr_t(pErr->GetsrcText()) +
            _bstr_t(L"\nLine: ") + _bstr_t(pErr->Getline()) +
            _bstr_t(L"\n");
    }
    else
    {
        bstrResult = _bstr_t(L"Validation succeeded for ") + bstrFile +
            _bstr_t(L"\n======================\n") +
            _bstr_t(pXD->xml) + _bstr_t(L"\n");
    }

CleanUp:
    return bstrResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = CoInitialize(NULL);
    if(SUCCEEDED(hr))
    {
        try
        {
            _bstr_t bstrOutput = validateFile(L"sc-valid.xml");
            bstrOutput += validateFile(L"sc-notValid.xml");
            MessageBoxW(NULL, bstrOutput, L"schemaCache",MB_OK);
        }
        catch(_com_error &e)
        {
            dump_com_error(e);
        }
        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 "schemaCacheProj" in the Project name field. For the project Location field, either accept the default setting or choose another location. Click OK.

  3. The Win32 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 schemaCacheProj files. From the File menu, select New.

  5. On the Files tab of the New dialog box, highlight C++ Source File. Then type "sc-valid.xml" in the File name text box, and click OK.

  6. Copy the first XML data file (sc-valid.xml), and paste it into the text file you just created.

  7. Repeat steps 5-6 for the second XML data file (sc-notValid.xml).

    Note

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

  8. Repeat steps 5-6 for the XSD schema file (sc.xsd).

  9. Repeat steps 5-6 for the C++ file above (schemaCache.cpp).

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

  11. Execute the sample application by selecting Start Debugging from the Debug menu.

  12. Verify that your output is the same as that listed in the output for this example.