Source: queryNodes.cpp

 

This example application calls both the selectSingleNode method and the selectNodes method on an XML DOM object loaded from the stocks.xml file. The same XPath expression is input to both methods:

//stock[1]/*  

This expression specifies all the child elements of the first <stock> element in the XML document. In MSXML, the selectSingleNode method returns the first element of the resultant node-set, and the selectNodes method returns all the elements in the node-set.

Programmatically, this source code performs the following steps:

  1. Creates an instance of an XML DOM object (pXMLDom).

  2. Calls the load method on pXMLDom to load XML data from a file (stocks.xml).

  3. Calls the selectSingleNode method on pXMLDom to select a DOM node according to the input XPath expression (//stock[1]/*). Then displays the resulting node in the console if the query is successful.

  4. Calls the selectNodes method on pXMLDom to select a DOM node-set according to the input XPath expression ("//stock[1]/*"). Then displays the resulting nodes in the console if the query is successful.

To add queryNodes.cpp to the project

  1. Select FileView on the project browser, and highlight queryNodeProj files. From the File menu, select New.

  2. On the Files tab of the New dialog box, highlight C++ Source File. Then type "queryNodes.cpp" in the File name text box, and click OK.

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

Next, we'll add the resource file to the queryNodes project.

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

// QueryNodes.cpp : Defines the entry point for the console application.
//

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

// Macro that calls a COM method returning HRESULT value.
#define CHK_HR(stmt)        do { hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)

// Macro to verify memory allcation.
#define CHK_ALLOC(p)        do { if (!(p)) { hr = E_OUTOFMEMORY; goto CleanUp; } } while(0)

// Macro that releases a COM object if not NULL.
#define SAFE_RELEASE(p)     do { if ((p)) { (p)->Release(); (p) = NULL; } } while(0)

// Helper function to create a VT_BSTR variant from a null terminated string. 
HRESULT VariantFromString(PCWSTR wszValue, VARIANT &Variant)
{
    HRESULT hr = S_OK;
    BSTR bstr = SysAllocString(wszValue);
    CHK_ALLOC(bstr);
    
    V_VT(&Variant)   = VT_BSTR;
    V_BSTR(&Variant) = bstr;

CleanUp:
    return hr;
}

// Helper function to create a DOM instance. 
HRESULT CreateAndInitDOM(IXMLDOMDocument **ppDoc)
{
    HRESULT hr = CoCreateInstance(__uuidof(DOMDocument60), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(ppDoc));
    if (SUCCEEDED(hr))
    {
        // these methods should not fail so don't inspect result
        (*ppDoc)->put_async(VARIANT_FALSE);  
        (*ppDoc)->put_validateOnParse(VARIANT_FALSE);
        (*ppDoc)->put_resolveExternals(VARIANT_FALSE);
        (*ppDoc)->put_preserveWhiteSpace(VARIANT_TRUE);
    }
    return hr;
}

// Helper function to display parse error.
// It returns error code of the parse error.
HRESULT ReportParseError(IXMLDOMDocument *pDoc, char *szDesc)
{
    HRESULT hr = S_OK;
    HRESULT hrRet = E_FAIL; // Default error code if failed to get from parse error.
    IXMLDOMParseError *pXMLErr = NULL;
    BSTR bstrReason = NULL;

    CHK_HR(pDoc->get_parseError(&pXMLErr));
    CHK_HR(pXMLErr->get_errorCode(&hrRet));
    CHK_HR(pXMLErr->get_reason(&bstrReason));
    printf("%s\n%S\n", szDesc, bstrReason);

CleanUp:
    SAFE_RELEASE(pXMLErr);
    SysFreeString(bstrReason);
    return hrRet;
}

void queryNodes()
{
    HRESULT hr = S_OK;
    IXMLDOMDocument *pXMLDom = NULL;
    IXMLDOMNodeList *pNodes = NULL;
    IXMLDOMNode *pNode = NULL;

    BSTR bstrQuery1 = NULL;
    BSTR bstrQuery2 = NULL;
    BSTR bstrNodeName = NULL;
    BSTR bstrNodeValue = NULL;
    VARIANT_BOOL varStatus;
    VARIANT varFileName;
    VariantInit(&varFileName);

    CHK_HR(CreateAndInitDOM(&pXMLDom));

    CHK_HR(VariantFromString(L"stocks.xml", varFileName));
    CHK_HR(pXMLDom->load(varFileName, &varStatus));
    if (varStatus != VARIANT_TRUE)
    {
        CHK_HR(ReportParseError(pXMLDom, "Failed to load DOM from stocks.xml."));
    }

    // Query a single node.
    bstrQuery1 = SysAllocString(L"//stock[1]/*");
    CHK_ALLOC(bstrQuery1);
    CHK_HR(pXMLDom->selectSingleNode(bstrQuery1, &pNode));
    if (pNode)
    {
        printf("Result from selectSingleNode:\n");
        CHK_HR(pNode->get_nodeName(&bstrNodeName));
        printf("Node, <%S>:\n", bstrNodeName);
        SysFreeString(bstrNodeName);
        
        CHK_HR(pNode->get_xml(&bstrNodeValue));
        printf("\t%S\n\n", bstrNodeValue);
        SysFreeString(bstrNodeValue);
        SAFE_RELEASE(pNode);
    }
    else
    {
        CHK_HR(ReportParseError(pXMLDom, "Error while calling selectSingleNode."));
    }

    // Query a node-set.
    bstrQuery2 = SysAllocString(L"//stock[1]/*");
    CHK_ALLOC(bstrQuery2);
    CHK_HR(pXMLDom->selectNodes(bstrQuery2, &pNodes));
    if(pNodes)
    {
        printf("Results from selectNodes:\n");
        //get the length of node-set
        long length;
        CHK_HR(pNodes->get_length(&length));
        for (long i = 0; i < length; i++)
        {
            CHK_HR(pNodes->get_item(i, &pNode));
            CHK_HR(pNode->get_nodeName(&bstrNodeName));
            printf("Node (%d), <%S>:\n", i, bstrNodeName);
            SysFreeString(bstrNodeName);

            CHK_HR(pNode->get_xml(&bstrNodeValue));
            printf("\t%S\n", bstrNodeValue);
            SysFreeString(bstrNodeValue);
            SAFE_RELEASE(pNode);
        }
    }
    else
    {
        CHK_HR(ReportParseError(pXMLDom, "Error while calling selectNodes."));
    }

CleanUp:
    SAFE_RELEASE(pXMLDom);
    SAFE_RELEASE(pNodes);
    SAFE_RELEASE(pNode);
    SysFreeString(bstrQuery1);
    SysFreeString(bstrQuery2);
    SysFreeString(bstrNodeName);
    SysFreeString(bstrNodeValue);
    VariantClear(&varFileName);
}

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

}