次の方法で共有


Create the Headers and Source Files for SaxJumpStart

 

In this topic, you will create the remaining headers and source files for the SaxJumpStart application. Create each file in Visual Studio, copy the code, and paste it into the file.

One of the most important activities when creating a Simple API for XML (SAX2) application is to implement handler classes. This example implements a ContentHandler that is derived from the ISAXContentHandler interface. The ContentHandler is implemented in MyContent.cpp.

Source Listing for MyContent.h

Source Listing for SAXContentHandlerImpl.h

Source Listing for SAXDTDHandlerImpl.h

Source Listing for SAXErrorHandlerImpl.h

Source Listing for MyContent.cpp

// MyContent.cpp: implementation of the MyContent class. 
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyContent.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MyContent::MyContent()
{
    idnt = 0;
}

MyContent::~MyContent()
{

}

HRESULT STDMETHODCALLTYPE MyContent::startElement( 
            /* [in] */ const wchar_t __RPC_FAR *pwchNamespaceUri,
            /* [in] */ int cchNamespaceUri,
            /* [in] */ const wchar_t __RPC_FAR *pwchLocalName,
            /* [in] */ int cchLocalName,
            /* [in] */ const wchar_t __RPC_FAR *pwchRawName,
            /* [in] */ int cchRawName,
            /* [in] */ ISAXAttributes __RPC_FAR *pAttributes)
{
    prt(L"<%s", pwchLocalName, cchLocalName);
    int lAttr;
    pAttributes->getLength(&lAttr);
    for(int i=0; i<lAttr; i++)
    {
        const wchar_t * ln;
        const wchar_t * vl;
        int lnl, vll;

        pAttributes->getLocalName(i,&ln,&lnl); 
        prt(L" %s=", ln, lnl);
        pAttributes->getValue(i,&vl,&vll);
        prt(L"\"%s\"", vl, vll);
    }
    printf(">"); 



    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE MyContent::characters( 
            /* [in] */ const wchar_t __RPC_FAR *pwchChars,
            /* [in] */ int cchChars)
{
    prt(L"%s", pwchChars, cchChars); 
    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE MyContent::endElement( 
            /* [in] */ const wchar_t __RPC_FAR *pwchNamespaceUri,
            /* [in] */ int cchNamespaceUri,
            /* [in] */ const wchar_t __RPC_FAR *pwchLocalName,
            /* [in] */ int cchLocalName,
            /* [in] */ const wchar_t __RPC_FAR *pwchRawName,
            /* [in] */ int cchRawName)
{
    prt(L"</%s>",pwchLocalName,cchLocalName); 
    return S_OK;
}

HRESULT STDMETHODCALLTYPE MyContent::startDocument()
{
    printf("<?xml version=\"1.0\" ?>");
    return S_OK;
}
        
void MyContent::prt(
            /* [in] */ const wchar_t * pwchFmt,
            /* [in] */ const wchar_t __RPC_FAR *pwchVal,
            /* [in] */ int cchVal)
{
    static wchar_t val[1000];
    cchVal = cchVal>999 ? 999 : cchVal;
    wcsncpy_s( val, pwchVal, cchVal );
    val[cchVal] = 0;
    wprintf(pwchFmt,val);
}

Source Listing for SAXContentHandlerImpl.cpp

// SAXContentHandlerImpl.cpp: implementation of the SAXContentHandlerImpl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SAXContentHandlerImpl.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


SAXContentHandlerImpl::SAXContentHandlerImpl()
{
}

SAXContentHandlerImpl::~SAXContentHandlerImpl()
{
}



HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::putDocumentLocator( 
            /* [in] */ ISAXLocator __RPC_FAR *pLocator
            )
{
    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::startDocument()
{
    return S_OK;
}
        

        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::endDocument( void)
{
    return S_OK;
}
        
        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::startPrefixMapping( 
            /* [in] */ const wchar_t __RPC_FAR *pwchPrefix,
            /* [in] */ int cchPrefix,
            /* [in] */ const wchar_t __RPC_FAR *pwchUri,
            /* [in] */ int cchUri)
{
    return S_OK;
}
        
        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::endPrefixMapping( 
            /* [in] */ const wchar_t __RPC_FAR *pwchPrefix,
            /* [in] */ int cchPrefix)
{
    return S_OK;
}
        

        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::startElement( 
            /* [in] */ const wchar_t __RPC_FAR *pwchNamespaceUri,
            /* [in] */ int cchNamespaceUri,
            /* [in] */ const wchar_t __RPC_FAR *pwchLocalName,
            /* [in] */ int cchLocalName,
            /* [in] */ const wchar_t __RPC_FAR *pwchRawName,
            /* [in] */ int cchRawName,
            /* [in] */ ISAXAttributes __RPC_FAR *pAttributes)
{
    return S_OK;
}
        
       
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::endElement( 
            /* [in] */ const wchar_t __RPC_FAR *pwchNamespaceUri,
            /* [in] */ int cchNamespaceUri,
            /* [in] */ const wchar_t __RPC_FAR *pwchLocalName,
            /* [in] */ int cchLocalName,
            /* [in] */ const wchar_t __RPC_FAR *pwchRawName,
            /* [in] */ int cchRawName)
{
    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::characters( 
            /* [in] */ const wchar_t __RPC_FAR *pwchChars,
            /* [in] */ int cchChars)
{
    return S_OK;
}
        

HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::ignorableWhitespace( 
            /* [in] */ const wchar_t __RPC_FAR *pwchChars,
            /* [in] */ int cchChars)
{
    return S_OK;
}
        

HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::processingInstruction( 
            /* [in] */ const wchar_t __RPC_FAR *pwchTarget,
            /* [in] */ int cchTarget,
            /* [in] */ const wchar_t __RPC_FAR *pwchData,
            /* [in] */ int cchData)
{
    return S_OK;
}
        
        
HRESULT STDMETHODCALLTYPE SAXContentHandlerImpl::skippedEntity( 
            /* [in] */ const wchar_t __RPC_FAR *pwchVal,
            /* [in] */ int cchVal)
{
    return S_OK;
}


long __stdcall SAXContentHandlerImpl::QueryInterface(const struct _GUID &riid,void ** ppvObject)
{
    return 0;
}

unsigned long __stdcall SAXContentHandlerImpl::AddRef()
{
    return 0;
}

unsigned long __stdcall SAXContentHandlerImpl::Release()
{
    return 0;
}

Source Listing for SAXDTDHandlerImpl.cpp

// ******************************************************************
//
//  testSax: example of MSXML SAX2/COM use and base classes for handlers
//  (C) Microsoft Corp., 2000
//
// ******************************************************************
//
// SAXDTDHandlerImpl.cpp: implementation of the SAXDTDHandlerImpl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SAXDTDHandlerImpl.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SAXDTDHandlerImpl::SAXDTDHandlerImpl()
{

}

SAXDTDHandlerImpl::~SAXDTDHandlerImpl()
{

}

HRESULT STDMETHODCALLTYPE SAXDTDHandlerImpl::notationDecl( 
            /* [in] */ const wchar_t __RPC_FAR *pwchName,
            /* [in] */ int cchName,
            /* [in] */ const wchar_t __RPC_FAR *pwchPublicId,
            /* [in] */ int cchPublicId,
            /* [in] */ const wchar_t __RPC_FAR *pwchSystemId,
            /* [in] */ int cchSystemId)
{
    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE SAXDTDHandlerImpl::unparsedEntityDecl( 
            /* [in] */ const wchar_t __RPC_FAR *pwchName,
            /* [in] */ int cchName,
            /* [in] */ const wchar_t __RPC_FAR *pwchPublicId,
            /* [in] */ int cchPublicId,
            /* [in] */ const wchar_t __RPC_FAR *pwchSystemId,
            /* [in] */ int cchSystemId,
            /* [in] */ const wchar_t __RPC_FAR *pwchNotationName,
            /* [in] */ int cchNotationName)
{
    return S_OK;
}


long __stdcall SAXDTDHandlerImpl::QueryInterface(const struct _GUID &,void ** )
{
    return 0;
}

unsigned long __stdcall SAXDTDHandlerImpl::AddRef()
{
    return 0;
}

unsigned long __stdcall SAXDTDHandlerImpl::Release()
{
    return 0;
}

Source Listing for SAXErrorHandlerImpl.cpp

// SAXErrorHandler.cpp: implementation of the SAXErrorHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SAXErrorHandlerImpl.h"
#include <stdio.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SAXErrorHandlerImpl::SAXErrorHandlerImpl()
{

}

SAXErrorHandlerImpl::~SAXErrorHandlerImpl()
{

}

HRESULT STDMETHODCALLTYPE SAXErrorHandlerImpl::error( 
            /* [in] */ ISAXLocator __RPC_FAR *pLocator,
            /* [in] */ const wchar_t * pwchErrorMessage,
            /* [in] */ HRESULT errCode)
{
    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE SAXErrorHandlerImpl::fatalError( 
            /* [in] */ ISAXLocator __RPC_FAR *pLocator,
            /* [in] */ const wchar_t * pwchErrorMessage,
            /* [in] */ HRESULT errCode)
{
    return S_OK;
}
        
HRESULT STDMETHODCALLTYPE SAXErrorHandlerImpl::ignorableWarning( 
            /* [in] */ ISAXLocator __RPC_FAR *pLocator,
            /* [in] */ const wchar_t * pwchErrorMessage,
            /* [in] */ HRESULT errCode)
{
    return S_OK;
}

long __stdcall SAXErrorHandlerImpl::QueryInterface(const struct _GUID &,void ** )
{
    return 0;
}

unsigned long __stdcall SAXErrorHandlerImpl::AddRef()
{
    return 0;
}

unsigned long __stdcall SAXErrorHandlerImpl::Release()
{
    return 0;
}