SOL9 Sample: SAXLexicalHandler
|
1 Screenshot
2 Source code
/*
* SAXLexicalHandler.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL9
// 2011/01/27
// 2011/02/18 Updated
// Sample program to read and parse an xml file by using SAXXMReader class
// and SAXLexicalHandler.
#include <sol/COMInitializer.h>
#include <sol/Locale.h>
#include <sol/xml/SAXXMLReader.h>
#include <sol/xml/SAXLexicalHandlerImpl.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 Lexicalhandler class based on the SAXLexicalHandlerImpl
class SAXLexicalHandler : public SAXLexicalHandlerImpl {
public:
SAXLexicalHandler ()
{
}
public:
virtual void startDTD (
__in const _bstr_t name,
__in const _bstr_t publicId,
__in const _bstr_t systemId)
{
printf("SAXLexicalHandler::startDTD() ");
printf("name=\"%S\" ", (const wchar_t*)name);
printf("publicId=\"%S\" ", (const wchar_t*)publicId);
printf("systemId=\"%S\"\n", (const wchar_t*)systemId);
}
virtual void startEntity (
__in const _bstr_t& name)
{
printf("SAXLexicalHandler::startEntify() name=\"%S\"\n", (const wchar_t*)name);
}
};
}
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;
SAXLexicalHandler lexicalHandler;
saxReader.putContentHandler(&contentHandler);
saxReader.putErrorHandler(&errorHandler);
saxReader.putDTDHandler(&dtdHandler);
saxReader.putLexicalHandler(&lexicalHandler);
saxReader.putFeatureProhibitDTD(VARIANT_FALSE); //2011/02/18
saxReader.putFeatureExternalGeneralEntities(VARIANT_TRUE); //2011/02/18
saxReader.putFeatureExternalParameterEntities(VARIANT_TRUE);//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.