SOL9 Sample: XMLDOMAttributes

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2009/04/24
// 2011/01/28 Updated

// Sample program to use XMLDOMDocument::getElementsByTagName() method and XMLDOMAttributes class. 

// Run as XMLAttributes.exe UserControl.xaml

#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 UserControl.xaml\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 xmlDocument;  __uuidof(MSXML2::DOMDocument60);
        XMLDOMDocument xmlDocument(__uuidof(MSXML2::DOMDocument30));

        xmlDocument.open(argv[1]);
        _bstr_t xml = xmlDocument.getXML();

        _tprintf(_T("XML:\n%s\n"), (const TCHAR*)xml);

        const TCHAR* name = _T("UserControl");

        _tprintf(_T("1 xmlDocument.getElementsByTagName(\"%s\")\n"), name);

        XMLDOMNodeList nodeList= xmlDocument.getElementsByTagName(name);;
        _tprintf(_T("2 OK, getElementsByTagName(length=%d)\n"), nodeList.getLength());
        XMLDOMNode node = nodeList.getItem(0);

        _tprintf(_T("3 Get the first Node \n"));
        XMLDOMAttributes attrs = node.getAttributes();

        _tprintf(_T("4 OK enumeates attributes\n"));
        _tprintf(_T("<%s\n"), name);
        int len = attrs.getLength();
        for (int i = 0; i<len; i++) {
            XMLDOMNode attr = attrs.getItem(i);
            
            _bstr_t name =attr.getNodeName();
            _bstr_t value = attr.getNodeValue();

            _tprintf(_T(" %s=\"%s\"\n"),
                    (const TCHAR*)name,
                    (const TCHAR*)value);
        }
        _tprintf(_T(" >\n"));

    } catch (HRESULT hr) {
        _tprintf(_T("Exception 0x%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.