HOW TO: Create a Stream of SAX Events from an Existing DOM Document Object by Using VC++ (310915)



The information in this article applies to:

  • Microsoft XML 4.0

This article was previously published under Q310915

SUMMARY

This step-by-step article describes how to create a stream of Simple API for XML (SAX) events from an existing Document Object Model (DOM) document object.

To create a stream of SAX events from an existing DOM document object, follow these steps:
  1. Create DOMDocument, SAXXMLReader, and MXXMLWriter objects.
  2. Set the XML writer object for the value of the ContentHandler property of the SAX reader object.
  3. Pass the DOM document object to the parse method of the SAX reader.
  4. Write the results of the SAX events to the output property of the SAX writer.
  5. Display the SAX writer output by using a message box.
back to the top

Create the Sample XML Document

  1. Use Notepad to create an XML document that contains the following:
    <?xml version="1.0"?>
    <!-- This file represents a fragment of a bookstore inventory database -->
    <bookstore>
      <book isbn="1-8790-098" Title="Beginning XML" Publisher="Wrox"/>
      <book isbn="1-8272-099" Title="Professional XML" Publisher="Wrox"/>
      <book isbn="1-2730-010" Title="XML Step by Step" Publisher="MS Press"/>  
    </bookstore>
  2. Save the document as C:\Books.xml.
back to the top

Create the Visual C++ Sample

  1. In Visual C++, create a new Win32 Console application.
  2. Paste the following code in the .cpp file:

    NOTE: See the inline comments to obtain an understanding of the functioning of the code.
    #include <atlbase.h>
    #import "c:\winnt\system32\msxml4.dll"
    
    using namespace MSXML2;
    
    void dump_com_error(_com_error &e);
    
    int main(int argc, char* argv[])
    {
          CoInitialize(NULL);
    
          USES_CONVERSION; //Handle the conversion from char to wchar_t.
    
          HRESULT hr = S_OK;
    
          try
          {
              IMXWriterPtr spWriter(__uuidof(MXXMLWriter40));
              IXMLDOMDocumentPtr spDoc(__uuidof(DOMDocument40));
              ISAXXMLReaderPtr spReader(__uuidof(SAXXMLReader40));                               
    
               //Load the DOM document.
               spDoc->async = VARIANT_FALSE;
               spDoc->validateOnParse = VARIANT_FALSE;
               spDoc->resolveExternals = VARIANT_FALSE;
                       
               // The relative path works in the debugger. Modify it with an absolute path as necessary.
    
                hr = spDoc->load((_variant_t)"books.xml");
    
                //Check for parse errors.
                if(hr!=VARIANT_TRUE)
                {
                      IXMLDOMParseErrorPtr  pError;
                      pError = pXMLDoc->parseError;
                      _bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
    
                      MessageBox(NULL,parseError, "Parse Error",MB_OK);
    
                      return 0;
    
                 }
    
                 else
                      MessageBox(NULL,pXMLDoc->xml, "Loading Succeeded",MB_OK);
    
     
                 //Set properties on the XML writer.
                 spWriter->PutbyteOrderMark(VARIANT_TRUE);
                 spWriter->Putindent(VARIANT_TRUE);
                 spWriter->PutomitXMLDeclaration(VARIANT_TRUE);
    
                 //Set the XML writer to the SAX content handler.
                 spReader->putContentHandler((ISAXContentHandlerPtr)spWriter);
                 spReader->putDTDHandler ((ISAXDTDHandlerPtr)spWriter);
                 spReader->putErrorHandler ((ISAXErrorHandlerPtr)spWriter);
                  spReader->putProperty(L"http://xml.org/sax/properties/lexical-handler", (_variant_t)spWriter.GetInterfacePtr ());
    
                  spReader->putProperty (L"http://xml.org/sax/properties/declaration-handler",(_variant_t)spWriter.GetInterfacePtr ());
    
                  spReader->parse((_variant_t)spDoc.GetInterfacePtr ());  
    
                  MessageBox(NULL, W2A(spWriter->output.bstrVal), "test", MB_OK); 
    
          }
    
          catch(_com_error &e)
          {
             dump_com_error(e);
             getchar();
             return -1;
          }
    
          return 0;
    
    }
    
     
    void dump_com_error(_com_error &e)
    
    {
          printf("Error\n");
          printf("\a\tCode = %08lx\n", e.Error());
          printf("\a\tCode meaning = %s", e.ErrorMessage());
    
          _bstr_t bstrSource(e.Source());
          _bstr_t bstrDescription(e.Description());
         
          printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
          printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
    
    }
  3. Compile and run the code. The SAX writer output is displayed in the message box.
back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

309822 HOW TO: Create a DOMDocument Object from SAX Events by Using Visual C++

307267 HOW TO: Secure XML Web Services with Secure Socket Layer in Windows 2000

back to the top

Modification Type:MajorLast Reviewed:10/26/2002
Keywords:kbDSupport kbhowto kbHOWTOmaster KB310915 kbAudDeveloper