IXMLDOMSchemaCollection/XMLSchemaCache

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

IXMLDOMSchemaCollection/XMLSchemaCache

Used by the schemas and namespaces properties of the IXMLDOMDocument2 interface.

[Script]

The following code shows how to create XMLSchemaCache objects in Microsoft® JScript®.

var cache = new ActiveXObject("Msxml2.XMLSchemaCache.5.0");

The following code shows how to create XMLSchemaCache objects in Microsoft® Visual Basic® Scripting Edition (VBScript).

Dim cache
Set cache = CreateObject("Msxml2.XMLSchemaCache.5.0");

Example

The following example shows how to build a reusable schema cache.

  1. Load a document that references schemas through x-schema.
    var xmldoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
    xmldoc.async = false;
    xmldoc.load("http://myserver/mydata.xml");
    if (xmlDoc.parseError.errorCode != 0) {
       var myErr = xmlDoc.parseError;
       alert("You have error " + myErr.reason);
    }
  2. Build a cache from the schemas loaded by your document.
    var cache = new ActiveXObject("Msxml2.XMLSchemaCache.5.0");
    cache.addCollection(xmldoc.namespaces);
  3. Now you can use this cache in the load method of another document.
    var xmldoc2 = new ActiveXObject("Msxml2.DOMDocument.5.0");
    xmldoc2.async = false;
    xmldoc2.schemas = cache;
    xmldoc2.load("http://myserver/newdata.xml");
    if (xmlDoc.parseError.errorCode != 0) {
       var myErr = xmlDoc.parseError;
       alert("You have error " + myErr.reason);
    }

    The Xmldoc2 object will load much faster because the schemas are cached.

[C/C++]

The IXMLDOMSchemaCollection implementation in MSXML 2.6 and 3.0 also supports the following interface through QueryInterface.

Interface Usage
IID_EnumVARIANT Returns an IEnumVARIANT implementation, so that the collection can be used in Microsoft® Visual Basic® For Each statements. Enumerates the namespace Uniform Resource Identifiers (URIs).

Return Values

E_FAIL
The attempt to modify a read-only object failed for one of the following reasons: argument is not a valid schema; invalid namespace URI; or document is not ready.
E_INVALIDARG
The parameter is incorrect.
E_OUTOFMEMORY
Out of memory.
E_POINTER
Invalid pointer.

Example

import "msxml5.dll"
using namespace MSXML2;

#define CHECK_AND_RELEASE(pInterface)  \
if(pInterface) \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

#define RELEASE(pInterface)  \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

BOOL DOMDocument2nameSpaces()
{
   BOOL bResult = FALSE;
   short sResult = FALSE;
   IXMLDOMElement *pIXMLDOMElement=NULL;
   IXMLDOMSchemaCollection *pIXMLDOMSchemaCollection=NULL;
   IXMLDOMDocument2 *pIXMLDOMDocument2=NULL;
   HRESULT hr;
   BSTR bstrValue;

   try
   {
      hr=CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER, 
         IID_IXMLDOMDocument2, (LPVOID*)(&pIXMLDOMDocument2));
      SUCCEEDED(hr) ? 0 : throw hr;

      if(pIXMLDOMDocument2)
      {
         hr=pIXMLDOMDocument2->put_async(VARIANT_FALSE);
         if(SUCCEEDED(hr))
         {
            hr=pIXMLDOMDocument2->load(_variant_t( 
               _T("d:\\inetpub\\wwwroot\\samplexmldtd.xml")), &sResult);
            if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
            {
               hr=pIXMLDOMDocument2->get_namespaces( 
                  &pIXMLDOMSchemaCollection);
               if(SUCCEEDED(hr))
               {
                  LONG uLength;

                  bResult=TRUE;
                  hr=pIXMLDOMSchemaCollection->get_length(&uLength);
                  if(SUCCEEDED(hr))
                  {
                     for(int iIndex=0; iIndex < uLength; iIndex++)
                     {
                        hr=pIXMLDOMSchemaCollection->get_namespaceURI( 
                           iIndex, &bstrValue);
                        if(SUCCEEDED(hr))
                           ::MessageBox(NULL, bstrValue, _T("Namespace"),
                           MB_OK);
                     }
                  }
               }
            }
         }
         RELEASE(pIXMLDOMDocument2);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXMLDOMDocument2);
      DisplayErrorToUser();
   }
   return bResult;
}

d:\\inetpub\\wwwroot\\samplexmldtd.xml

<?xml version='1.0'?>
<COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE>
  <BOOK>
    <TITLE>Lover Birds</TITLE>
    <AUTHOR>Cynthia Randall</AUTHOR>
    <PUBLISHER>Lucerne Publishing</PUBLISHER>
  </BOOK>
</COLLECTION>

Output (in a message box)

urn:schemas-microsoft-com:datatypes

Remarks

The IXMLDOMSchemaCollection/XMLSchemaCache object is free-threaded and can be used in multiple documents at the same time. The XML Schema document that is sent to the schema collection is cloned, and the XML Schema document remains writable. Any changes that occur in the XML Schema file after it is stored in the schema cache are not reflected in its cloned image. A single schema cache can be added to multiple schema collections, due to the creation of the cloned image.

Note In MSXML, "free-threaded" means ThreadingModel='Both', and cross-thread marshalling is supported.

Versioning

MSXML 2.6 and later

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

Header and IDL files: msxml2.h, msxml2.idl

Version-Dependent ProgID: Msxml2.XMLSchemaCache.5.0

Version-Dependent CLSID: 88d969c2-f192-11d4-a65f-0040963251e5

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

IXMLDOMSchemaCollection/XMLSchemaCache Members | IXMLDOMDocument2