Share via


IXMLDOMSchemaCollection-XMLSchemaCache

 

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

JScript Examples

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

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

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

// Load a document that references schemas through x-schema.  
var xmldoc = new ActiveXObject("Msxml2.DOMDocument.6.0");  
xmldoc.async = false;  
xmldoc.load("book.xml");  
if (xmldoc.parseError.errorCode != 0) {  
   var myErr = xmldoc.parseError;  
   WScript.Echo("You have error " + myErr.reason);  
} else {  
   WScript.Echo(xmldoc.xml);  
}  
// Build a cache from the schemas loaded by your document.  
var cache = new ActiveXObject("Msxml2.XMLSchemaCache.3.0");  
cache.addCollection(xmldoc.namespaces);  
  
// Now you can use this cache in the load method of another document.  
var xmldoc2 = new ActiveXObject("Msxml2.DOMDocument.6.0");  
xmldoc2.async = false;  
xmldoc2.schemas = cache;  
xmldoc2.load("book2.xml");  
if (xmldoc2.parseError.errorCode != 0) {  
   var myErr2 = xmldoc2.parseError;  
   WScript.Echo("You have error " + myErr2.reason);  
} else {  
   WScript.Echo(xmldoc2.xml);  
}  
  

The xmldoc2 object will load much faster because its schemas are cached.

IEnumVariant

The IXMLDOMSchemaCollection implementation in Microsoft XML Core Services (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.

C++ Example

#include “msxml6.h”  
  
#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("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;  
}  
  

XML Resource file: samplexmldtd.xml

The example uses the following XML file.

<?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

The example outputs the following 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.

Requirements

Implementation:

msxml3.dll, msxml2.lib (MSXML 3.0)

msxml6.dll, msxml6.lib (MSXML 6.0)

Header and IDL files:

msxml2.h, msxml2.idl, msxml6.h, msxml6.idl

Versioning

Implemented in:

MSXML 3.0, MSXML 6.0

See Also

IXMLDOMSchemaCollection-XMLSchemaCache Members
IXMLDOMDocument2