Microsoft XML 2.5 SDK


 

IXMLDOMDocument::onreadystatechange Event

[This is preliminary documentation and subject to change.]

Runs when the readystate property changes.

C/C++ Syntax

HRESULT onreadystatechange(void);

C/C++ Return Value

Returns S_OK if successful, or an error code otherwise.

Remarks

The event handler can be set by explicitly setting the onreadystatechange property.

This member is an extension of the W3C DOM.

Visual Basic Example

The following Visual Basic example shows how to use the onreadystatechange event in an application:

Dim WithEvents xmldoc As DOMDocument

Private Sub Form_Load()
    Set xmldoc = New DOMDocument 
    xmldoc.Load ("http://xmlweb/msxml/jscript/error.xml")
End Sub

Private Sub xmldoc_onreadystatechange()
    If (xmldoc.readyState = 4) Then 
       msg = "Load  is finished" & Chr(13) 
       If (xmldoc.parseError.errorCode <> 0) Then
            msg = msg + "Error: " & xmldoc.parseError.reason
       End If
       MsgBox msg
    End If
End Sub

C/C++ Example

BEGIN_DISPATCH_MAP(CXMLDOMSamples, CCmdTarget)
   DISP_FUNCTION_ID(CXMLDOMSamples, "Ready State Event Handler", DISPID_XMLDOMEVENT_ONREADYSTATECHANGE, DOMDocCheckState, VT_EMPTY, 0)
END_DISPATCH_MAP()

class CXMLDOMSamples : public CCmdTarget
{
private:
   HRESULT m_hr;
   IXMLDOMDocument* m_pIXMLDOMDocument;
   BSTR m_bstrXMLDocName;
   DWORD m_pdwCookie;
public:
   BOOL AdviseConnectionPoint(BOOL bAdvise = TRUE);
   void Caller();
   BOOL DOMDocLoadLocation();
   BOOL DOMDocStateChangeSetup();
   void DOMDocCheckState();
};

// Assumptions   
// -    m_pIXMLDOMDocument has already been intialised (a DOMDocument object has been created ).
// -   Automation is enabled for the class to enable it to connect to the Connection point (DIID_XMLDOMDocumentEvents).

void CXMLDOMSamples::Caller()
{
   try
{
   m_bstrXMLDocName = ::SysAllocString(_T("http://MyServer/sample.xml"));
   DOMDocStateChangeSetup();
   DOMDocLoadLocation();
   ::SysFreeString(m_bstrXMLDocName);
   m_bstrXMLDocName = NULL;
}
   catch(...)
   {
      if(m_bstrXMLDocName)
   ::SysFreeString(m_bstrXMLDocName);
      DisplayErrorToUser();
   } 
}
BOOL CXMLDOMSamples::AdviseConnectionPoint(BOOL bAdvise)
{
   BOOL bResult = FALSE;
   try
   {
      IConnectionPointContainer *pIConnectionPointContainer = NULL;
      IConnectionPoint *pCP = NULL;

m_hr = m_pIXMLDOMDocument->QueryInterface    (IID_IConnectionPointContainer, (void**)&pIConnectionPointContainer);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;
   
   If(pIConnectionPointContainer)
   {
m_hr = pIConnectionPointContainer->FindConnectionPoint (DIID_XMLDOMDocumentEvents, &pCP);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;
      if(SUCCEEDED(m_hr) && pCP)
      {
      if(bAdvise)
         m_hr = pCP->Advise(GetIDispatch(TRUE), &m_pdwCookie);
      else
         m_hr = pCP->Unadvise(m_pdwCookie);
      bResult = SUCCEEDED(m_hr) ? TRUE : FALSE;
      pCP->Release();
      pCP = NULL;
}
   pIConnectionPointContainer->Release();
      pIConnectionPointContainer = NULL;
}
   }   
   catch(...)
   {
      if(pIConnectionPointContainer)
         pIConnectionPointContainer->Release();
      if(pCP)
         pCP->Release();
      DisplayErrorToUser();
   }
   return bResult;
}
BOOL CXMLDOMSamples::DOMDocStateChangeSetup()
{
   BOOL bResult = FALSE;
   try
   {
      m_hr = m_pIXMLDOMDocument->put_async(VARIANT_TRUE);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;
      bResult = AdviseConnectionPoint();
   }
   catch(...)
   {
      DisplayErrorToUser();
   }
   return bResult;
}
BOOL CXMLDOMSamples::DOMDocLoadLocation()
{
   short sResult = FALSE;
   BOOL bResult = FALSE;
   try
   {
      _variant_t varString = m_bstrXMLDocName;
      m_hr = m_pIXMLDOMDocument->load(varString, &sResult);
      bResult = SUCCEEDED(m_hr) ? TRUE : FALSE;
   }
   catch(...)
   {
      DisplayErrorToUser();
   }
   return bResult;
}
void CXMLDOMSamples::DOMDocCheckState()
{
IXMLDOMParseError *pIParseError = NULL;
  long value;

try
   {
     m_hr = m_pIXMLDOMDocument->get_readyState(&value);
     SUCCEEDED(m_hr) ? 0 : throw m_hr;
   if(value == 4 )
   {
      m_hr = m_pIXMLDOMDocument->get_parseError(&pIParseError);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;
      m_hr = pIParseError->get_errorCode(&value);
      pIParseError->Release();
      pIParseError=NULL;
      if(!SUCCEEDED(m_hr))
      {
         throw m_hr;
      }
      m_strOutput = (value != 0) ? _T("Load Error") : _T("Load Succeeded");
   }
   }
   catch(...)
   {
      if(pIParseError)
         pIParseError->Release();
      DisplayErrorToUser();
   }
}