SOL9 2.0 Sample: XMLAttributes

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * XMLAttributes.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/04/24

// Sample program to use XMLDocument::getElementsByTagName() method and XMLAttributes class. 

// Run as XMLAttributes.exe UserControl.xaml

#include <sol/COMInitializer.h>
#include <sol/xml/XMLDocument.h>


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

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

    try {
        COMInitializer comInitializer;

        {
            SOL::XMLDocument xmlDocument;
            xmlDocument.open(argv[1]);
            _bstr_t xml = "";
            xmlDocument.getXML(xml);

            _tprintf(_T("XML:\n%s\n"), (const TCHAR*)xml);
            
            const TCHAR* name = _T("UserControl");
            _tprintf(_T("1 xmlDocument.getElementsByTagName(\"%s\")\n"), name);
    
            XMLNodeList nodeList;

            if (xmlDocument.getElementsByTagName(name, nodeList)) {
                _tprintf(_T("2 OK, getElementsByTagName\n"));
                XMLNode node;
                //Get the first node!    
                if (nodeList.getItem(0, node)) {
                    _tprintf(_T("3 Get the first Node \n"));
                    XMLAttributes attrs;
                    if (node.getAttributes(attrs)) {
                        _tprintf(_T("4 OK enumeates attributes\n"));
                        _tprintf(_T("<%s\n"), name);
                        int len = attrs.getLength();
                        for (int i = 0; i<len; i++) {
                            _bstr_t name = "";
                            _bstr_t value = "";
                            attrs.getItem(i, name, value);
                            _tprintf(_T(" %s=\"%s\"\n"),
                                (const TCHAR*)name,
                                (const TCHAR*)value);
                        }
                        _tprintf(_T(" >\n"));
                    } else {
                        _tprintf(_T("5 Failed to getAttributes\n"));                        
                    } 

                } else {
                    _tprintf(_T("6 Not found a XMLNode\n"));
                }
            } else {
                _tprintf(_T("7 Failed to getElementsByTagName\n"));

            }
        }

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

}

Last modified: 11 Nov 2009

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