// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
// standard C++ headers
#include <iostream>
#include <cstdlib>

using namespace std;

// Windows headers
#include <windows.h>
#include <tchar.h>
#include <comutil.h>
#include <atlconv.h>
#include <msxml.h>

const wchar_t *XML_FILE = L"employees.xml";

void ReturnCheck(LPTSTR func, HRESULT res)
{
    if(res != S_OK)
    {
        TCHAR buffer[1000];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, res, 0, buffer, sizeof(buffer), 0);
        cout << func << ": " << buffer << endl; 
        exit(1);
    }
}

int main()
{
    USES_CONVERSION;
    HRESULT res;
    VARIANT_BOOL res2;
    //
    CoInitialize(NULL);
    // read from file to DOM tree
    IXMLDOMDocument *doc;
    res = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&doc);
    ReturnCheck(_T("CoCreateInstance"), res);
    doc->put_async(false);
    res = doc->load(_variant_t(XML_FILE), &res2);
    ReturnCheck(_T("load"), res);
    // iterate over all employee elements
    IXMLDOMNodeList *employees;
    res = doc->selectNodes(L"//employee[@no>1 and role!='Intern']", &employees);
    ReturnCheck(_T("selectNodes"), res);
    long len;
    res = employees->get_length(&len);
    ReturnCheck(_T("get_length"), res);
    IXMLDOMNode *xemployee;
    for(int i = 0; i < len; i++)
    {
        res = employees->get_item(i, &xemployee);  
        ReturnCheck(_T("get_item"), res);
        IXMLDOMElement *employee;
        res = xemployee->QueryInterface(__uuidof(IXMLDOMElement), (void **)&employee);
        ReturnCheck(_T("QueryInterface"), res);
        // find attribute no
        IXMLDOMNode *nonode;
        res = xemployee->selectSingleNode(L"@no", &nonode);
        ReturnCheck(_T("selectSingleNode"), res);
        _variant_t xno;
        res = nonode->get_nodeValue(&xno);
        ReturnCheck(_T("get_nodeValue"), res);
        int no = atoi(W2A((wchar_t *)(_bstr_t)xno));
        // find sub elements name and role
        IXMLDOMNode *namenode;
        res = xemployee->selectSingleNode(L"name/text()", &namenode);
        ReturnCheck(_T("selectSingleNode"), res);
        _variant_t xname;
        res = namenode->get_nodeValue(&xname);
        ReturnCheck(_T("get_nodeValue"), res);
        char *name = W2A((wchar_t *)(_bstr_t)xname);
        IXMLDOMNode *rolenode;
        res = xemployee->selectSingleNode(L"role/text()", &rolenode);
        ReturnCheck(_T("selectSingleNode"), res);
        _variant_t xrole;
        res = rolenode->get_nodeValue(&xrole);
        ReturnCheck(_T("get_nodeValue"), res);
        char *role = W2A((wchar_t *)(_bstr_t)xrole);
        // print
        cout << "no = " << no << endl;
        cout << "name = " << name << endl;
        cout << "role = " << role << endl;
        //
        nonode->Release();
        nonode = NULL;
        namenode->Release();
        namenode = NULL;
        rolenode->Release();
        rolenode = NULL;
        employee->Release();
        employee = NULL;
    }
    // cleanup
    employees->Release();
    employees = NULL;
    doc->Release();
    doc = NULL;
    //
    CoUninitialize();
    return 0;
}
