// 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->getElementsByTagName(L"employee", &employees);
    ReturnCheck(_T("getElementsByTagName"), 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
        _variant_t xno;
        res = employee->getAttribute(L"no", &xno);
        ReturnCheck(_T("getAttribute"), res);
        int no = atoi(W2A((wchar_t *)(_bstr_t)xno));
        // iterate over all employee elements
        IXMLDOMNodeList *children;
        res = employee->get_childNodes(&children);
        ReturnCheck(_T("get_childNodes"), res);
        _variant_t xname;
        _variant_t xrole;
        char *name;
        char *role;
        long len2;
        res = children->get_length(&len2);
        ReturnCheck(_T("get_length"), res);
        IXMLDOMNode *child;
        for(int j = 0; j < len2; j++)
        {
            res = children->get_item(j, &child);
            ReturnCheck(_T("get_item"), res);
            DOMNodeType typ;
            res = child->get_nodeType(&typ);
            if(typ == NODE_ELEMENT)
            {
                BSTR nam;
                res = child->get_nodeName(&nam);
                ReturnCheck(_T("get_nodeName"), res);
                if(wcscmp((wchar_t *)nam, L"name") == 0)
                {
                    IXMLDOMNode *txt;
                    res = child->get_firstChild(&txt);
                    ReturnCheck(_T("get_firstChild"), res);
                    res = txt->get_nodeValue(&xname);
                    ReturnCheck(_T("get_nodeValue"), res);
                    name = W2A((wchar_t *)(_bstr_t)xname);
                    txt->Release();
                    txt = NULL;
                }
                if(wcscmp((wchar_t *)nam, L"role") == 0)
                {
                    IXMLDOMNode *txt;
                    res = child->get_firstChild(&txt);
                    ReturnCheck(_T("get_firstChild"), res);
                    res = txt->get_nodeValue(&xrole);
                    ReturnCheck(_T("get_nodeValue"), res);
                    role = W2A((wchar_t *)(_bstr_t)xrole);
                    txt->Release();
                    txt = NULL;
                }
            }
            child->Release();
            child = NULL;
        }
        // print
        cout << "no = " << no << endl;
        cout << "name = " << name << endl;
        cout << "role = " << role << endl;
        //
        children->Release();
        children = NULL;
        employee->Release();
        employee = NULL;
    }
    // add employee
    IXMLDOMNode *retnode;
    IXMLDOMText *txtnode;
    IXMLDOMElement *newname;
    res = doc->createElement(L"name", &newname);
    ReturnCheck(_T("createElement"), res);
    res = doc->createTextNode(L"Dave D", &txtnode);
    ReturnCheck(_T("createTextNode"), res);
    res = newname->appendChild(txtnode, &retnode);
    ReturnCheck(_T("appendChild"), res);
    IXMLDOMElement *newrole;
    res = doc->createElement(L"role", &newrole);
    ReturnCheck(_T("createElement"), res);
    res = doc->createTextNode(L"Intern", &txtnode);
    ReturnCheck(_T("createTextNode"), res);
    res = newrole->appendChild(txtnode, &retnode);
    ReturnCheck(_T("appendChild"), res);
    IXMLDOMElement *newemployee;
    res = doc->createElement(L"employee", &newemployee);
    ReturnCheck(_T("createElement"), res);
    res = newemployee->setAttribute(L"no",  _variant_t(L"4"));
    ReturnCheck(_T("setAttribute"), res);
    res = newemployee->appendChild(newname, &retnode);
    ReturnCheck(_T("appendChild"), res);
    res = newemployee->appendChild(newrole, &retnode);
    ReturnCheck(_T("appendChild"), res);
    IXMLDOMElement *rootnode;
    res = doc->get_documentElement(&rootnode);
    ReturnCheck(_T("get_documentElement"), res);
    res = rootnode->appendChild(newemployee, &retnode);
    ReturnCheck(_T("appendChild"), res);
    newname->Release();
    newname = NULL;
    newrole->Release();
    newrole = NULL;
    newemployee->Release();
    newemployee = NULL;
    rootnode->Release();
    rootnode = NULL;
    // write out
    BSTR xml;
    res = doc->get_xml(&xml);
    ReturnCheck(_T("get_xml"), res);
    cout << W2A((wchar_t *)xml) << endl;
    // cleanup
    employees->Release();
    employees = NULL;
    doc->Release();
    doc = NULL;
    //
    CoUninitialize();
    return 0;
}
