HOW TO: Create a DOMDocument Object from SAX Events by Using Visual C++ (309822)
The information in this article applies to:
This article was previously published under Q309822 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:
- Create the DOMDocument and MXXMLWriter objects.
- Set the MXXMLWriter object as the ContentHandler.
- Set the output property of the MXXMLWriter object as the DOMDocument object.
- Send the proper sequence of events to the ContentHandler to build the instance of the provided document.
- 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:
#include "atlbase.h"
//change the path to point to msxml4.dll if necessary
#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 = 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[]= "version='1.0' encoding='UTF-16' standalone='no'";
//Step 1: Create the XML declaration.
hr = spContentHandler->processingInstruction(L"xml", 3, A2W(szPI), strlen(szPI));
//Step 2: Create the books element.
hr = spContentHandler->startElement(L"", 0, L"books",5, L"books", 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("", L"title", L"title","","Beginning XML");
//Implicitly QI on IMXAttributes to get back the ISAXAttributes interface pointer.
ISAXAttributesPtr spAttributes;
spAttributes= pMXAttr;
// B. Create the book element.
hr = spContentHandler->startElement(L"", 0, L"book",4, L"book", 4, spAttributes);
//End of book element.
hr = spContentHandler->endElement(L"",0,L"book", 4, L"book",4);
//Step 4: Add another book element.
//clear the attribute collection
hr = pMXAttr->clear();
hr = pMXAttr->addAttribute("", L"title", L"title","",L"Professional XML");
spAttributes= pMXAttr;
hr = spContentHandler->startElement(L"", 0, L"book",4, L"book", 4, spAttributes);
hr = spContentHandler->endElement(L"",0,L"book", 4, L"book",4);
//Step 5: End of books element.
hr = spContentHandler->endElement(L"",0,L"books", 5, L"books",5); hr = spContentHandler->endDocument ();
MessageBox(NULL, spXMLDoc->xml, "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);
}
- Compile and run the code. The XML that you created is displayed in the message box.
back to the top
REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
310915 HOW TO: Create a Stream of SAX Events from an Existing DOM Document Object by Using VC++
back to the top
Modification Type: | Major | Last Reviewed: | 10/26/2002 |
---|
Keywords: | kbDSupport kbhowto kbHOWTOmaster KB309822 kbAudDeveloper |
---|
|