SOL9 Sample: SAXDeclHandler

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2011/01/27

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

#include <sol/COMInitializer.h>
#include <sol/Locale.h>
#include <sol/xml/SAXXMLReader.h>
#include <sol/xml/SAXDeclHandlerImpl.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::startElment() LocalName=\"%S\"\n", (const wchar_t*)localName);
  }

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 Declhandler class based on the SAXDeclHandlerImpl

class SAXDeclHandler : public SAXDeclHandlerImpl {
public:
  SAXDeclHandler()
  {
  }

public:
  virtual void elementDecl (
    __in const _bstr_t name,
    __in const _bstr_t model)
  {
    printf("SAXDeclHandler::elementDecl() ");
    printf("Name=%S ", (const wchar_t*)name);
    printf("Model=%S", (const wchar_t*)model);
    printf("\n");  
  }

  virtual void attributeDecl (
    __in const _bstr_t elementName,
    __in const _bstr_t attributeName,
    __in const _bstr_t type,
    __in const _bstr_t valueDefault,
    __in const _bstr_t value)
  {
    printf("SAXDeclHandler::attributeDecl() ");
    printf("ElementName=%S ", (const wchar_t*)elementName);
    printf("AttributeName=%S ", (const wchar_t*)attributeName);
    printf("Type=%S ", (const wchar_t*)type);
    printf("ValueDefault=%S ", (const wchar_t*)valueDefault);
    printf("value=%S ", (const wchar_t*)value);
    printf("\n");
  }
};

}


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;
  }
    
  COMInitializer comInitializer;
  Locale locale;

  try {
    _bstr_t fileName = argv[1];

    SOL::SAXXMLReader saxReader;

    SAXContentHandler contentHandler;
    
    SAXErrorHandlerImpl errorHandler;
    SAXDTDHandlerImpl dtdHandler;
    SAXDeclHandler declHandler;

    saxReader.putContentHandler(&contentHandler);
    saxReader.putErrorHandler(&errorHandler);
    saxReader.putDTDHandler(&dtdHandler);
    saxReader.putDeclHandler(&declHandler);
    
    saxReader.putFeatureProhibitDTD(VARIANT_FALSE);  //2011/02/18

    _tprintf(_T("1 Try to read and parse an xml file %s\n"), argv[1]);

    saxReader.parseURL((const wchar_t*)fileName);

    _tprintf(_T("2 OK parased\n"));

  } 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 0x%x\n"), GetLastError());
  }

}

Last modified: 2 May 2016

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