⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlhandler.cpp

📁 手机天气预报系统
💻 CPP
字号:
/*
 ============================================================================
 Name		: XmlHander.cpp
 Author	  : Richie Chyi
 Copyright   : Copyright?2008
 Description : CXmlHandler implementation.
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <f32file.h>
#include <stringpool.h>  // For RString
#include "XmlHandler.h"
 
// CONSTANTS
_LIT8( KXmlMimeType, "text/xml" );
 
// METHODS DEFINITION
 
CXmlHandler* CXmlHandler::NewL(RArray<TWeatherDetail>& aDetails, MXmlParseObserver& aObserver)
    {
    CXmlHandler* self = CXmlHandler::NewLC(aDetails, aObserver);
    CleanupStack::Pop();
    return self;
    }
 
CXmlHandler* CXmlHandler::NewLC(RArray<TWeatherDetail>& aDetails, MXmlParseObserver& aObserver)
    {
    CXmlHandler* self = new ( ELeave ) CXmlHandler(aDetails, aObserver);
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }
 
CXmlHandler::~CXmlHandler()
    {
    delete iParser;
    delete iBuffer;
    }
 
CXmlHandler::CXmlHandler(RArray<TWeatherDetail>& aDetails, MXmlParseObserver& aObserver)
    :iWeatherDetails(aDetails),iObserver(aObserver)
    {
    }
 
void CXmlHandler::ConstructL()
    {
    iParser = CParser::NewL( KXmlMimeType, *this );
    iBuffer = NULL;
    iHasNewElement = EFalse;
    }
 
void CXmlHandler::StartParsingL( const TDesC& aBuffer )
    {
    delete iBuffer;
    iBuffer = NULL;
    iBuffer = HBufC8::NewL( aBuffer.Length() );
    TPtr8 bufferPtr( iBuffer->Des() );
    bufferPtr.Copy(aBuffer);
    
    // Tell the parser that we are about to parse a XML document.    
    iParser->ParseBeginL();
    iParser->ParseL(*iBuffer);
    }
 
void CXmlHandler::OnStartDocumentL( const RDocumentParameters& /*aDocParam*/,
        TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        // Do something here when the parser at the start of the document.
        }
    else
        {
        // Do something if error happens.
        }
    }
 
void CXmlHandler::OnEndDocumentL( TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        // Do something here when the parser reaches the end of the document.
        }
    }
 
void CXmlHandler::OnStartElementL( const RTagInfo& aElement,
        const RAttributeArray& aAttributes, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        // Found start of an element, for example: "<tag>"
        // The name of the element is stored in aElement.LocalName().DesC().
 
        // Do something with the start of an element.
        const TDesC8& localName = aElement.LocalName().DesC();
        
        // 如果是新XML串,则首先清空数组
        if (0 == localName.Compare(KElementRoot))
        	{
        	iWeatherDetails.Reset();
        	}
        
    	// 新节点,增加一元素
        if(0 == localName.Compare(KElementForecast))
    		{
    		TWeatherDetail newDetail;
    		iWeatherDetails.AppendL(newDetail);
    		
    		iHasNewElement = ETrue;
    		}
     
        if(aAttributes.Count() && iHasNewElement)
        	{
        	const TDesC8& data = aAttributes[0].Value().DesC();

        	if(0 == localName.Compare(KElementDayOfWeak))
        		{
        		iWeatherDetails[iWeatherDetails.Count()-1].iDayOfWeek.Copy(data);
        		}
        	else if(0 == localName.Compare(KElementLow))
        		{
        		iWeatherDetails[iWeatherDetails.Count()-1].iLow.Copy(data);
        		}
        	else if(0 == localName.Compare(KElementHigh))
	        	{
	        	iWeatherDetails[iWeatherDetails.Count()-1].iHigh.Copy(data);
        		}
        	else if(0 == localName.Compare(KElementIcon))
        		{
        		iWeatherDetails[iWeatherDetails.Count()-1].iIcon.Copy(data);
        		}
        	else if(0 == localName.Compare(KElementCondition))
        		{
        		iWeatherDetails[iWeatherDetails.Count()-1].iCondition.Copy(data);
        		
        		// 最后元素,重置iHasNewElement
        		iHasNewElement = EFalse;
        		}
        	}
        }
    else
        {
        // Do something if error happens.
        iParser->ParseEndL();
        }
    }
        
void CXmlHandler::OnEndElementL( const RTagInfo &aElement, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        // Found the end of an element, for example: "</tag>"
        // The name of the element is stored in aElement.LocalName().DesC().
 
        // Do something with the end of an element.
       	const TDesC8& localName = aElement.LocalName().DesC();
      	if(0 == localName.Compare(KElementRoot))
      		{
      		 iObserver.ParseCompleted();
      		}
        }
    else
        {
        // Do something if error happens.
        }
    }
    
void CXmlHandler::OnContentL( const TDesC8 &/*aBytes*/, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        // aBytes stored the value of the parsed contents.
        }
    else
        {
        // Display error messages here.
        }
    }
    
void CXmlHandler::OnStartPrefixMappingL( const RString& /*aPrefix*/,
        const RString& /*aUri*/, TInt /*aErrorCode*/ )
    {
    }
        
void CXmlHandler::OnEndPrefixMappingL( const RString& /*aPrefix*/,
        TInt /*aErrorCode*/ )
    {
    }
    
void CXmlHandler::OnIgnorableWhiteSpaceL( const TDesC8& /*aBytes*/,
        TInt /*aErrorCode*/ )
    {
    }
    
void CXmlHandler::OnSkippedEntityL( const RString& /*aName*/,
        TInt /*aErrorCode*/ )
    {
    }
 
void CXmlHandler::OnProcessingInstructionL( const TDesC8& /*aTarget*/,
        const TDesC8& /*aData*/, TInt /*aErrorCode*/ )
    {
    }
 
void CXmlHandler::OnError( TInt /*aErrorCode*/ )
    {
    // Do something if error happens.
    }
 
TAny* CXmlHandler::GetExtendedInterface( const TInt32 /*aUid*/ )
    {
    return 0;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -