Microsoft KB Archive/309822

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

Article ID: 309822

Article Last Modified on 10/26/2002

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q309822



IN THIS TASK

 * SUMMARY
 * Create a DOMDocument Object from SAX Events Using Visual C++
 * Steps to Build the Sample
 * REFERENCES



SUMMARY
MSXML 4.0 adds two new features to the integration of Simple API for XML (SAX) and the Document Object Model (DOM). This article demonstrates how to create a DOMDocument object from SAX events in Microsoft Visual C++.

back to the top

Create a DOMDocument Object from SAX Events by Using Visual C++
To create the DOMDocument object, follow these steps:
 * 1) Create the DOMDocument and MXXMLWriter objects.
 * 2) Set the MXXMLWriter object as the ContentHandler.
 * 3) Set the output property of the MXXMLWriter object as the DOMDocument object.
 * 4) Send the proper sequence of events to the ContentHandler to build the instance of the provided document.
 * 5) Use DOM methods to return the name of the author and display the name in a message box.

back to the top

Steps to Build the Sample
 In Visual C++, create a new Win32 Console application.  Paste the following code in a .cpp file. See the inline comments for a description of how the code functions:
 * 1) include &quot;atlbase.h&quot;

//change the path to point to msxml4.dll if necessary using namespace MSXML2;
 * 1) import &quot;c:\winnt\system32\msxml4.dll&quot;

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 = NULL; hr = spWriter.CreateInstance(__uuidof(MXXMLWriter40)); IXMLDOMDocumentPtr spXMLDoc = NULL;

hr = spXMLDoc.CreateInstance(__uuidof(DOMDocument40));

//Set the indentation to True.

spWriter->indent = VARIANT_TRUE;

//Implicit QI on Writer to get back the ISAXContentHandler //interface pointer. ISAXContentHandlerPtr spContentHandler=NULL; spContentHandler = spWriter; spWriter->Putoutput(spXMLDoc.GetInterfacePtr);

//Start the document by adding the XML declaration. hr = spContentHandler->startDocument ; char szPI[]= &quot;version='1.0' encoding='UTF-16' standalone='no'&quot;;

//Step 1: Create the XML declaration. hr = spContentHandler->processingInstruction(L&quot;xml&quot;, 3, A2W(szPI), strlen(szPI));

//Step 2: Create the books element. hr = spContentHandler->startElement(L&quot;&quot;, 0, L&quot;books&quot;,5, L&quot;books&quot;, 5, NULL);

IMXAttributesPtr pMXAttr; hr = pMXAttr.CreateInstance(__uuidof(SAXAttributes30));

//Step 3: Create a book element with attribute of title. //A. Create the attribute.

hr = pMXAttr->addAttribute(&quot;&quot;, L&quot;title&quot;, L&quot;title&quot;,&quot;&quot;,&quot;Beginning XML&quot;);

//Implicitly QI on IMXAttributes to get back the ISAXAttributes interface pointer.

ISAXAttributesPtr spAttributes; spAttributes= pMXAttr;

// B. Create the book element.

hr = spContentHandler->startElement(L&quot;&quot;, 0, L&quot;book&quot;,4, L&quot;book&quot;, 4, spAttributes);

//End of book element. hr = spContentHandler->endElement(L&quot;&quot;,0,L&quot;book&quot;, 4, L&quot;book&quot;,4);

//Step 4: Add another book element. //clear the attribute collection

hr = pMXAttr->clear; hr = pMXAttr->addAttribute(&quot;&quot;, L&quot;title&quot;, L&quot;title&quot;,&quot;&quot;,L&quot;Professional XML&quot;);

spAttributes= pMXAttr; hr = spContentHandler->startElement(L&quot;&quot;, 0, L&quot;book&quot;,4, L&quot;book&quot;, 4, spAttributes);

hr = spContentHandler->endElement(L&quot;&quot;,0,L&quot;book&quot;, 4, L&quot;book&quot;,4);

//Step 5: End of books element. hr = spContentHandler->endElement(L&quot;&quot;,0,L&quot;books&quot;, 5, L&quot;books&quot;,5);          hr = spContentHandler->endDocument ; MessageBox(NULL, spXMLDoc->xml, &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);

}

 Compile and run the code. The XML that you created is displayed in the message box.

back to the top

