SOL9 Sample: SAXXMLReaderApplet

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SAXXMLReaderApplet.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 SAXXMReader class.

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

#include <sol/xml/SAXXMLReader.h>

namespace SOL {

// SAXXMLReader Console Application using an internal class SAXContentHandler 
//included in SAXXMLReaderApplet derived from SAXXMLReader.

class SAXXMLReaderApplet :public SAXXMLReader {

private:
  //<InnerClass> ---------------------------------------------
  //Derive your own Contenthandler class from SAXContentHandlerImpl.
  class SAXContentHandler: public SAXContentHandlerImpl {
  public:
    SAXContentHandler() { 
    }

  public:
    virtual void startDocument() {
      printf("SAXContentHandler::startDocument()\n"); 
    }

    virtual void processingInstruction(
    __in const _bstr_t target,
    __in const _bstr_t data) {
      printf("SAXContentHandler::processingInstruction() Target=\"%S\" Data=\"%S\"\n",
        (const wchar_t*)target, (const wchar_t*)data); 
    }

    virtual void endDocument() {
      printf("SAXContentHandler::endDocument()\n");
    }

    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() Uri=\"%S\" LocalName=\"%S\" QName=\"%S\"\n", 
      (const wchar_t*)uri,
      (const wchar_t*)localName,
      (const wchar_t*)qName);
    }

    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);
      }
    }

    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);
    }
  };
  //</InnerClass>---------------------------------------------


private:
  //Instance member of the above internal class
  SAXContentHandler contentHandler;


public:
  /**
   * SAXXMLReaderApplet Constructor
   */
  SAXXMLReaderApplet()
  {
    this->putContentHandler(&contentHandler);
  }

  void start(const _bstr_t& fileName)
  {
    parseURL((const wchar_t*)fileName);  
  }
};

}


void _tmain(int argc, TCHAR** argv)
{
  if (argc !=2) {
    _tprintf(_T("Usage:\n%s sample.xaml or books.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];

    SAXXMLReaderApplet applet;
    applet.start(fileName);

  } 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: 2 May 2016

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