SOL9 Sample: SAXSchemaDeclHandler

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2011/01/27

// Sample program to read and parse an xml file by using SAXXMReader class.

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

#include <sol/xml/SAXXMLReader.h>
#include <sol/xml/XMLDOMDocument.h>

#include <sol/xml/SAXSchemaReader.h>

#include <sol/xml/SchemaElement.h>
#include <sol/xml/SAXAttributes.h>

namespace SOL {

//Please define your own Contenthandler class based on the SAXContentHandlerImpl
class SAXContentHandler: public SAXContentHandlerImpl {

public:
    SAXContentHandler()
    {
    }

public:
    virtual void startElement(
        __in const _bstr_t uri,
        __in const _bstr_t localName,
        __in const _bstr_t qName,
        __in struct MSXML2::ISAXAttributes * pAttributes)
    {
        printf("SAXContentHandler::startElement() Uri=\"%S\" LocalName=\"%S\" QName=\"%S\"\n", 
            (const wchar_t*)uri,
            (const wchar_t*)localName,
            (const wchar_t*)qName);
                
    }

public:
    virtual void characters(
        __in const _bstr_t chars)
    {
        if (chars.length()>0) {
            printf("SAXContentHandler::characters(length:%d) = \"%S\"\n", chars.length(), (const wchar_t*)chars);
        }
    }

public:
    virtual void endElement(
        __in const _bstr_t uri,
        __in const _bstr_t localName,
        __in const _bstr_t qName)
    {
        printf("SAXContentHandler::endElement() LocalName=\"%S\"\n", (const wchar_t*)localName);
    }
};


//Please define your own SchemaDeclHandler class based on the SAXSchemaDeclHandlerImpl

class SAXSchemaDeclHandler: public SAXSchemaDeclHandlerImpl {

public:
    SAXSchemaDeclHandler()
    {
    }

public:
    virtual void schemaElementDecl(
        __in struct MSXML2::ISchemaElement * oSchemaElement)
    {
        try {
            printf("SAXSchemaDeclHandler::schemaElementDecl()\n");
            SchemaElement element = oSchemaElement;
            element.display();
        } catch (...) {
        }
    }
};

}



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

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

    COMInitializer comInitializer;
    Locale locale;

    _bstr_t xsdFile = argv[1];     //sample.xsd
    _bstr_t xmlFile = argv[2];        // sample.xml
    
    try {
        SAXSchemaReader schemaReader;
        schemaReader.read(xsdFile);
        _tprintf(_T("1 OK SAXSchemaReader::read(%s)\n"), (const TCHAR*)xsdFile);
        
        _bstr_t targetNamespace  = schemaReader.getTargetNamespace();

        _tprintf(_T("2 TargetNamespace=%s\n"), (const TCHAR*)targetNamespace);

        SOL::SAXXMLReader saxReader;

        SOL::SAXContentHandler contentHandler;
        _tprintf(_T("3 Created an object of SAXContentHandler\n"));        
        
        SOL::XMLDOMSchemaCollection schemaCollection;
        _tprintf(_T("4 Created an object of SchemaCollection\n"));

        schemaCollection.putValidateOnLoad(VARIANT_TRUE);

        schemaCollection.add(targetNamespace, _variant_t(xsdFile) );
        _tprintf(_T("5 OK. Added an xsdFile=%s to SchemaCollection object with targetNamespace=%s\n"), 
            xsdFile, (const TCHAR*)targetNamespace);
        schemaCollection.display();

        schemaCollection.validate();
        _tprintf(_T("6 OK. Validated xsd=%s\n"), xsdFile);
        
        SOL::SAXSchemaDeclHandler schemaDeclHandler;
        _tprintf(_T("7 OK Created SchemaDeclHandler\n"));

        saxReader.putContentHandler(&contentHandler);

        _tprintf(_T("8 OK Put ContentHandler to SAXReader\n"));

        saxReader.putSchemaDeclHandler(&schemaDeclHandler);

        _tprintf(_T("9 OK Put SchemaDeclHandler to SAXReader\n"));

        saxReader.putSchemaCollection(&schemaCollection);

        _tprintf(_T("10 OK Put SchemaCollection to SAXReader\n"));

        saxReader.enableSchemaValidation();
        _tprintf(_T("11 OK Enabled SchemaValidation\n"));

        saxReader.parseURL(xmlFile);

        _tprintf(_T("12 OK Parsed xml=%s \n"),  argv[2]);

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

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