SOL9 Sample: XMLPathFinderWithNamespace

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * XMLPathFinderWithNamespace.cpp 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/07/23
// 2011/01/28 Modified to use XMLDOMDocument not XMLPathFinder

// Sample program to use XMLPathFinder::selectNode() and selectNodes() methods
// for manifest xml with a namespace.


#include <sol/COMInitializer.h>
#include <sol/Locale.h>
#include <sol/xml/XMLDOMDocument.h>


void _tmain(int argc, TCHAR** argv)
{
    if (argc !=2) {
        _tprintf(_T("Usage:\n%s manifest.xml\n"), argv[0]);
        return;
    }

    if (GetFileAttributes(argv[1]) == 0xffffffff) {
        _tprintf(_T("File not found %s\n"), argv[1]);
        return;
    }

    try {
        Locale locale;
        COMInitializer comInitializer;
        {
            XMLDOMDocument pathFinder;

            pathFinder.setProperty(_bstr_t("SelectionNamespaces"), 
                _variant_t(L"xmlns:ns='urn:schemas-microsoft-com:asm.v1'"));

            pathFinder.open(argv[1]);
            _bstr_t xml = pathFinder.getXML();
            _tprintf(_T("XML:\n%s\n"), (const TCHAR*)xml);

            const TCHAR* xpath = _T("//ns:dependency/ns:dependentAssembly/ns:assemblyIdentity[@type=\"win32\"]");

            _tprintf(_T("1 pathFinder.selectSingleNode(\"%s\")\n"), xpath);
            try {
                XMLDOMNode node = pathFinder.selectSingleNode(xpath);
                _tprintf(_T("1.1 OK, selectSingleNode\n"));
                node.dump();
            } catch (Exception& ex) {
                ex.printf();
                _tprintf(_T("1.2 Not found\n"));
            } catch (...) {
                _tprintf(_T("1.3 Not found\n"));
            }

            pathFinder.setProperty(_bstr_t("SelectionNamespaces"),
                _variant_t(L"xmlns:ns2='urn:schemas-microsoft-com:asm.v3'"));

            const TCHAR* xpath2 = _T("//ns2:security/ns2:requestedPrivileges/ns2:requestedExecutionLevel");

            _tprintf(_T("2 pathFinder.selectNodes(\"%s\")\n"), xpath2);
            try {
                XMLDOMNodeList nodeList = pathFinder.selectNodes(xpath2);
                _tprintf(_T("2.1 OK, selectNodes\n"));
                nodeList.dump();
            } catch (Exception& ex) {
                ex.printf();
                _tprintf(_T("2.2 Not found\n"));
            }catch (...) {
                _tprintf(_T("2.3 Not found\n"));
            }
        }

    } catch (HRESULT hr) {
        _tprintf(_T("Exception %x\n"), hr);
    } catch (Exception& ex) {
        ex.printf();
    } catch (...) {
        _tprintf(_T("Exception %x\n"), GetLastError());
    }

}

Last modified: 25 Feb 2011

Copyright (c) 2011 Antillia.com ALL RIGHTS RESERVED.