Share via


Source: dynamDOMsmart.cpp

 

This application creates a simple, but complete, XML DOM object, with <root> as the document element. This element contains three child elements: <node1>, <node2>, and <node3>. The first child element contains character data. The second child element contains a CDATA section. The last child element contains three empty child elements: <subnode1>, <subnode2>, and <subnode3>.

Programmatically, the dynamDOMsmart application performs the following steps:

  1. Creates an XML DOM instance (pXMLDom).

  2. Calls the createProcessInstruction method on pXMLDom. This creates a processing instruction node (pi) targeted for XML 1.0.

  3. Calls the appendChild method on pXMLDom. This adds the processing instruction node (pi) to pXMLDom.

  4. Calls the createComment method on the DOM object (pXMLDom) to create a comment node (pc) and then append it pXMLDom.

  5. Creates a <root> element (pRoot) as the document element, with an attribute whose name is "created" and value "using DOM". Adds this element (<root>) to the DOM object (pXMLDom).

  6. Creates a <node1> element with some character data as its content. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).

  7. Creates a <node2> element that contains a CDATA section (pcd) with markup text. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).

  8. Creates a <node3> element that contains a DOM document fragment (pdf). This fragment contains three other empty child elements: <subNode1>, <subNode2>, and <subNode3>. The code then appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).

  9. Saves this dynamically created DOM object to the project's main directory, and prints the XML data in the application console.

C/C++ Source File (dynamDOMsmart.cpp)

#include <stdio.h>
#include <tchar.h>
#import <msxml6.dll>

void dynamDOMsmart()
{
    MSXML2::IXMLDOMDocumentPtr pXMLDom;
    HRESULT hr = pXMLDom.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER);
    if (FAILED(hr)) 
    {
        printf("Failed to instantiate an XML DOM.\n");
        return;
    }

    try
    {
        pXMLDom->async = VARIANT_FALSE;
        pXMLDom->validateOnParse = VARIANT_FALSE;
        pXMLDom->resolveExternals = VARIANT_FALSE;
        pXMLDom->preserveWhiteSpace = VARIANT_TRUE;

        // Create a processing instruction targeted for xml.
        MSXML2::IXMLDOMProcessingInstructionPtr pi = pXMLDom->createProcessingInstruction(L"xml", L"version='1.0'");
        pXMLDom->appendChild(pi);

        // Create a comment for the document.
        MSXML2::IXMLDOMCommentPtr pc = pXMLDom->createComment(L"sample xml file created using XML DOM object.");
        pXMLDom->appendChild(pc);

        // Create the root element (i.e., the documentElement).
        MSXML2::IXMLDOMElementPtr pRoot = pXMLDom->createElement(L"root");

        // Create a "created" attribute for the root element and
        // assign the "using dom" character data as the attribute value.
        MSXML2::IXMLDOMAttributePtr pa = pXMLDom->createAttribute(L"created");
        pa->value = L"using dom";
        pRoot->setAttributeNode(pa);

        // Next, we will create and add more nodes to the root element 
        // we've just created.

        // Create an element to hold text content.
        MSXML2::IXMLDOMElementPtr pe = pXMLDom->createElement(L"node1");
        // Add newline + tab for indentation.
        pRoot->appendChild(pXMLDom->createTextNode(L"\n\t"));
        pe->text = L"some character data";
        pRoot->appendChild(pe);

        // Create an element to hold a CDATA section.
        pe = pXMLDom->createElement(L"node2");
        // Add newline + tab for indentation.
        pRoot->appendChild(pXMLDom->createTextNode(L"\n\t"));
        pe->appendChild(pXMLDom->createCDATASection(L"<some mark-up text>"));
        pRoot->appendChild(pe);

        // Create an element to hold three empty subelements.
        pe = pXMLDom->createElement(L"node3");
        // Add newline +tab for indentation.
        pRoot->appendChild(pXMLDom->createTextNode(L"\n\t"));

        MSXML2::IXMLDOMDocumentFragmentPtr pdf = pXMLDom->createDocumentFragment();
        pdf->appendChild(pXMLDom->createTextNode(L"\n\t\t"));
        pdf->appendChild(pXMLDom->createElement(L"subNode1"));
        pdf->appendChild(pXMLDom->createTextNode(L"\n\t\t"));
        pdf->appendChild(pXMLDom->createElement(L"subNode2"));
        pdf->appendChild(pXMLDom->createTextNode(L"\n\t\t"));
        pdf->appendChild(pXMLDom->createElement(L"subNode3"));
        pdf->appendChild(pXMLDom->createTextNode(L"\n\t"));
        pe->appendChild(pdf);
        pRoot->appendChild(pe);

        pRoot->appendChild(pXMLDom->createTextNode(L"\n"));

        // Add the root element to the DOM instance.
        pXMLDom->appendChild(pRoot);

        printf("Dynamically created DOM:\n%s\n", static_cast<PCSTR>(pXMLDom->xml));

        hr = pXMLDom->save(L"dynaDOMsmart.xml");
        if (SUCCEEDED(hr)) 
        {
            printf("DOM saved to dynaDOMsmart.xml\n");
        }
        else
        {
            printf("Failed to save DOM to dynaDomsmart.xml\n");
        }
    }
    catch (_com_error errorObject)
    {
        printf("exception thrown, HRESULT: 0x%08x", errorObject.Error());
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = CoInitialize(NULL);
    if (SUCCEEDED(hr))
    {
        dynamDOMsmart();
        CoUninitialize();
    }
    return 0;
}

To add the dynamDOM source code to the project

  1. Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the new file dynamDOM.cpp.

  2. Copy the C/C++ source code above and paste it into the source file you just created.

  3. Next, build and run the dynamDOM project. The result should be the output shown in the following topic.