Microsoft KB Archive/310915

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

Article ID: 310915

Article Last Modified on 10/26/2002

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q310915



IN THIS TASK
SUMMARY
 * Create the Sample XML Document
 * Create the Visual C++ Sample

REFERENCES



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
  Use Notepad to create an XML document that contains the following: 

    Save the document as C:\Books.xml.

back to the top

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

NOTE: See the inline comments to obtain an understanding of the functioning of the code.
 * 1) include <atlbase.h>
 * 2) import &quot;c:\winnt\system32\msxml4.dll&quot;

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)&quot;books.xml&quot;);

//Check for parse errors. if(hr!=VARIANT_TRUE) {                 IXMLDOMParseErrorPtr  pError; pError = pXMLDoc->parseError; _bstr_t parseError =_bstr_t(&quot;At line &quot;)+ _bstr_t(pError->Getline) + _bstr_t(&quot;\n&quot;)+ _bstr_t(pError->Getreason);

MessageBox(NULL,parseError, &quot;Parse Error&quot;,MB_OK);

return 0;

}

else MessageBox(NULL,pXMLDoc->xml, &quot;Loading Succeeded&quot;,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&quot;http://xml.org/sax/properties/lexical-handler&quot;, (_variant_t)spWriter.GetInterfacePtr );

spReader->putProperty (L&quot;http://xml.org/sax/properties/declaration-handler&quot;,(_variant_t)spWriter.GetInterfacePtr );

spReader->parse((_variant_t)spDoc.GetInterfacePtr );

MessageBox(NULL, W2A(spWriter->output.bstrVal), &quot;test&quot;, MB_OK);

}

catch(_com_error &e) {        dump_com_error(e); getchar; return -1; }

return 0;

}

void dump_com_error(_com_error &e)

{     printf(&quot;Error\n&quot;); printf(&quot;\a\tCode = %08lx\n&quot;, e.Error); printf(&quot;\a\tCode meaning = %s&quot;, e.ErrorMessage);

_bstr_t bstrSource(e.Source); _bstr_t bstrDescription(e.Description); printf(&quot;\a\tSource = %s\n&quot;, (LPCSTR) bstrSource); printf(&quot;\a\tDescription = %s\n&quot;, (LPCSTR) bstrDescription);

} </li> Compile and run the code. The SAX writer output is displayed in the message box.</li></ol>

back to the top

<div class="references_section">