SOL9 Sample: XMLPathFinder

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * XMLPathFinder.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/04/23
// 2011/01/28 Modified to use XMLDOMDocument class not XMLPathFinder.

// Sample program to use XMLDOMDocument::selectSingleNode() and selectNodes() methods.

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

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

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

  try {
    
    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:MainWindow/ns:LayoutManager/ns:Button[@name=\"Cancel\"]");

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

    const TCHAR* xpath1 = _T("//MainWindow/*/RichText");

    _tprintf(_T("2 pathFinder.selectSingleNode(\"%s\")\n"), xpath1);
    try {
      XMLDOMNode node = pathFinder.selectSingleNode(xpath1);
      _tprintf(_T("2.1 OK, selectNode\n"));
      node.dump();
    } catch (Exception& ex) {
      ex.printf();
      _tprintf(_T("2.2 Not found\n"));
    } catch (_com_error& ex) {
      ComError error(ex);
      error.printf();
      _tprintf(_T("2.3 Not found\n"));
    } catch (...) {
      _tprintf(_T("2.4 Not found\n"));
    }

    const TCHAR* xpath2 = _T("//MainWindow/*/Button");
    _tprintf(_T("3 pathFinder.selectNodes(\"%s\")\n"), xpath2);
    try {
      XMLDOMNodeList nodeList =  pathFinder.selectNodes(xpath2);//, nodeList)) {
      _tprintf(_T("3.1 OK, selectNodes\n"));
      nodeList.dump();
    } catch (Exception& ex) {
      ex.printf();
      _tprintf(_T("3.2 Not found\n"));
    } catch (_com_error& ex) {
      ComError error(ex);
      error.printf();
      _tprintf(_T("3.3 Not found\n"));
    } catch (...) {
      _tprintf(_T("3.4 Not found\n"));
    }

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

}

Last modified: 2 May 2016

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