xml的解析

// --------------------------------------------------------------------------
// XmlHandler.h
//
// Copyright 2007, Antony Pranata
// http://www.antonypranata.com
//
// Example how to parse XML file using SAX on Symbian OS.
//
// There are two methods to parse XML document, i.e.;
//   - StartParsingL() -> parse XML document without active object.
//     The whole file content is read at once. It is simple, but suitable
//     for relatively small file only.
//   - StartParsingWithAoL() -> parse XML document with active object.
// --------------------------------------------------------------------------
#ifndef __XMLHANDLER_H__
#define __XMLHANDLER_H__
// INCLUDE FILES
#include <e32base.h>
#include <xml/contenthandler.h> // for MContentHandler
#include <xml/parser.h> // for CParser
// CLASS DECLARATION
using namespace Xml;
#include "XML_Data.h"
#include "TXMLDataType.h"
/**
 * MXmlHandlerObserver, an observer to CXmlHandler class.
 */
class MXmlHandlerObserver
    {
public:
    virtual void OnParseCompleted( TInt aError,RPointerArray <XML_Data> &aXMLDataArray, RPointerArray <TXMLDataType> aXMLDataTypeArray, TInt aNum) = 0;
    };
/**
 * CXmlHandler, a class to parse XML file and then output log information
 * to a buffer.
 */
class CXmlHandler: public CActive, MContentHandler
    {
    enum TState
        {
        EIdle,
        EReadingFile
        };
       
public: // Constructors and destructor
   
    static CXmlHandler* NewL( MXmlHandlerObserver& aObserver );
   
    static CXmlHandler* NewLC( MXmlHandlerObserver& aObserver );
   
    virtual ~CXmlHandler();
   
public: // Public methods
    void StartParsingL( const TDesC& aFileName );
   
    void StartParsingWithAoL( const TDesC& aFileName , TInt aNum);
   
    TPtr DisplayResult()
    { return iDisplayResult->Des(); }
private: // Constructors
    CXmlHandler( MXmlHandlerObserver& aObserver );
   
    void ConstructL();
private: // from CActive
   
    void DoCancel();
   
    void RunL();
   
    TBuf8<20> iBuf;
private: // from MContentHandler
    void OnStartDocumentL( const RDocumentParameters &aDocParam, TInt aErrorCode );
   
    void OnEndDocumentL( TInt aErrorCode );
   
    void OnStartElementL( const RTagInfo &aElement, const RAttributeArray &aAttributes,
        TInt aErrorCode );
       
    void OnEndElementL( const RTagInfo &aElement, TInt aErrorCode );
   
    void OnContentL( const TDesC8 &aBytes, TInt aErrorCode );
   
    void OnStartPrefixMappingL( const RString &aPrefix, const RString &aUri,
        TInt aErrorCode );
       
    void OnEndPrefixMappingL( const RString &aPrefix, TInt aErrorCode );
   
    void OnIgnorableWhiteSpaceL( const TDesC8 &aBytes, TInt aErrorCode );
   
    void OnSkippedEntityL( const RString &aName, TInt aErrorCode );
   
    void OnProcessingInstructionL( const TDesC8 &aTarget, const TDesC8 &aData,
        TInt aErrorCode);
       
    void OnError( TInt aErrorCode );
   
    TAny *GetExtendedInterface( const TInt32 aUid );
public:
 void Reset();
 HBufC* GetSysnewText(){return iSysnewText;}
private: // Private data
    TInt iLength;
    MXmlHandlerObserver& iObserver;
    CParser*             iParser;
    HBufC8*              iBuffer;
    HBufC*               iDisplayResult;
    TState               iState;
    RFile                iFile;
   
    RPointerArray <XML_Data> iXMLDataArray; 
    RPointerArray <TXMLDataType> iXMLDataTypeArray; 
    XML_Data *iXMLData;
    TXMLDataType *iXMLDataType;
   
 TBool iFlag;
 HBufC* iSysnewText;
 TInt iNum;
    };
#endif /* __XMLHANDLER_H__ */
// End of File
 
// --------------------------------------------------------------------------
// XmlHandler.cpp
//
// Copyright 2007, Antony Pranata
// http://www.antonypranata.com
//
// Example how to parse XML file using SAX on Symbian OS.
//
// There are two methods to parse XML document, i.e.;
//   - StartParsingL() -> parse XML document without active object.
//     The whole file content is read at once. It is simple, but suitable
//     for relatively small file only.
//   - StartParsingWithAoL() -> parse XML document with active object.
// --------------------------------------------------------------------------
// INCLUDE FILES
#include <coemain.h>
#include <f32file.h>
#include "XmlHandler.h"
#include <aknnotewrappers.h>
#include <EscapeUtils.h>
// CONSTANTS
const TInt KBufferSize = 1024; // buffer size to store the result of
const TInt KFileBufferSize = 1024; // buffer size for file reading
_LIT(KOpeningTag, "<" );
_LIT(KClosingTag, ">" );
_LIT(KSlash, "/" );
_LIT(KEndOfLine, "/f" );
// CEikEdwin uses '/f' as EOF mark.
_LIT8( KXmlMimeType, "text/xml" );
// METHODS DEFINITION
// --------------------------------------------------------------------------
_LIT8(KMname_zh,"Mname_zh");
_LIT8(KMname_en,"Mname_en");
_LIT8(KZip,"zip");
_LIT8(KPath,"path");
_LIT8(KJurnal,"jurnal");
_LIT8(KImage,"image");
_LIT8(KDescription,"description");
_LIT8(KMsize,"Msize");
_LIT8(KSysText,"systext");
_LIT8(KBigTypeName,"MbigTypeName");
_LIT8(KBigTypeId,"MbigId");
_LIT8(KSmallTypeName,"MsmallTpyeName");
_LIT8(KSmallTypeId,"MsmallId");
#include <UTF.H>
#define _MAGZINE_TYPE 5
CXmlHandler* CXmlHandler::NewL(MXmlHandlerObserver& aObserver)
 {
 CXmlHandler* self = CXmlHandler::NewLC(aObserver);
 CleanupStack::Pop();
 return self;
 }
// --------------------------------------------------------------------------
CXmlHandler* CXmlHandler::NewLC(MXmlHandlerObserver& aObserver)
 {
 CXmlHandler* self = new (ELeave) CXmlHandler(aObserver);
 CleanupStack::PushL(self);
 self->ConstructL();
 return self;
 }
// --------------------------------------------------------------------------
CXmlHandler::~CXmlHandler()
 {
 Cancel();
 if(iParser)
 {
  delete iParser;
  iParser = NULL;
 }
 if(iDisplayResult)
 {
  delete iDisplayResult;
  iDisplayResult = NULL;
 }
 if(iBuffer)
 {
  delete iBuffer;
  iBuffer=NULL;
 }
 if(iSysnewText)
 {
  delete iSysnewText;
  iSysnewText=NULL;
 }
 
 if(iXMLData)
 {
  delete iXMLData;
  iXMLData=NULL;
 }
 
 if(iXMLDataType)
 {
  delete iXMLDataType;
  iXMLDataType=NULL;
 }
 
 iXMLDataArray.ResetAndDestroy();
 iXMLDataTypeArray.ResetAndDestroy();
 iFile.Close();
 }
// --------------------------------------------------------------------------
CXmlHandler::CXmlHandler(MXmlHandlerObserver& aObserver) :
 CActive(EPriorityStandard), iObserver(aObserver), iParser(0),
 iDisplayResult(0), iState(EIdle) 
 {
 CActiveScheduler::Add(this);
 }
// --------------------------------------------------------------------------
void CXmlHandler::DoCancel()
 {
 iParser->ParseEndL();
 iFile.Close();
 if(iBuffer)
 {
 delete iBuffer;
 iBuffer = 0;
 }
 }
void CXmlHandler::Reset()
 {
 iXMLDataArray.ResetAndDestroy();
 iXMLDataTypeArray.ResetAndDestroy();
 }
// --------------------------------------------------------------------------
void CXmlHandler::RunL()
 {
 if (KErrNone == iStatus.Int())
  {
  // If the buffe length is zero, it means if we have reached
  // the end of the file.
  if (iBuffer->Length() == 0)
   {
   iParser->ParseEndL();
   iFile.Close();
   if(iBuffer)
   {
    delete iBuffer;
    iBuffer = 0;
   }
   iObserver.OnParseCompleted(iStatus.Int(), iXMLDataArray, iXMLDataTypeArray, iNum);
   }
  // Otherwise, we continue reading the next chunk of the XML file.
  else
   {
   // Parse the next "part" of the XML document.
   iParser->ParseL(*iBuffer);
   // Read the next chunk of the file.
   TPtr8 bufferPtr(iBuffer->Des());
   iFile.Read(bufferPtr, KFileBufferSize, iStatus);
   // Don't forget to call this... :)
   SetActive();
   }
  }
 else
  {
  //iObserver.OnParseCompleted(iStatus.Int());
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::ConstructL()
 {
 iParser = CParser::NewL(KXmlMimeType, *this);
 iDisplayResult = HBufC::NewL(KBufferSize);
 
 iXMLDataType = TXMLDataType::NewL();
 iXMLData = XML_Data::NewL();
 iFlag = EFalse;
 iSysnewText = HBufC::NewL(0);
 }
// --------------------------------------------------------------------------
void CXmlHandler::StartParsingL(const TDesC& aFileName)
 {
 // Read the whole content of aFileName into iBuffer.
 // IMPORTANT: This example reads the whole content within
 // one-shot for simplicity reason.
 // In reality, you have to read it chunk by chunk
 // using active object so that your application doesn't block.
 RFile file;
 User::LeaveIfError(file.Open(CCoeEnv::Static()->FsSession(), aFileName,
   EFileRead));
 CleanupClosePushL(file);
 iDisplayResult->Des().Zero();
 TInt size;
 User::LeaveIfError(file.Size(size));
 if(iBuffer)
 {
  delete iBuffer;
  iBuffer = 0;
 }
 iBuffer = HBufC8::NewL(size);
 TPtr8 bufferPtr(iBuffer->Des());
 User::LeaveIfError(file.Read(bufferPtr));
 CleanupStack::PopAndDestroy(); // file
 // Now, we have the whole file content in iBuffer.
 // We are ready to parse the XML content.
 iParser->ParseBeginL();
 iParser->ParseL(*iBuffer);
 // Since we read the whole file contents within one-shot,
 // we can call ParseEndL() right after calling ParseL().
 iParser->ParseEndL();
 //iObserver.OnParseCompleted(0);
 }
// --------------------------------------------------------------------------
void CXmlHandler::StartParsingWithAoL(const TDesC& aFileName, TInt aNum)
 {
 // Remember to cancel any outstanding request first.
 if (IsActive())
  {
  Cancel();
  }
    iNum = aNum;
 User::LeaveIfError(iFile.Open(CCoeEnv::Static()->FsSession(), aFileName,
   EFileRead));
 // Create a buffer to store the file content.
 // Note that this method uses active object to read the file.
 // So we have to call SetActive() at the end. Then we call CParser::ParseL()
 // in RunL() method.
 iDisplayResult->Des().Zero();
 if(iBuffer)
 {
  delete iBuffer;
  iBuffer = 0;
 }
 iBuffer = HBufC8::NewL(KFileBufferSize);
 TPtr8 bufferPtr(iBuffer->Des());
 iFile.Read(bufferPtr, KFileBufferSize, iStatus);
 SetActive();
 // Tell the parser that we are about to parse a XML document.   
 iParser->ParseBeginL();
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnStartDocumentL(const RDocumentParameters& /*aDocParam*/,
  TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnEndDocumentL(TInt aErrorCode)
 {
 // Display message to the screen stating that this is the starting of
 // XML document.
 _LIT( KOnEndDocumentL, "*** OnEndDocumentL ***/f" );
 if (KErrNone == aErrorCode)
 {
 }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnStartElementL(const RTagInfo& aElement,
  const RAttributeArray& aAttributes, TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  iFlag = ETrue;
  iBuf.Copy(aElement.LocalName().DesC());
  }
 else
  {
  //iObserver.OnParseCompleted(aErrorCode);
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnEndElementL(const RTagInfo &aElement, TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnContentL(const TDesC8 &aBytes, TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  if(!iFlag)
  {
   return ;
  }
  if(0 == aBytes.Length())
  {
   return;
  }
  if(0 == aBytes.Compare(_L8("/n")))
  {
   return;
  }
     if (0 == aBytes.Compare(_L8("/t")))
  {
   return ;
  }
     
     if (0 == aBytes.Compare(_L8("/t/t")))
  {
   return ;
  }
     
  iLength = aBytes.Length() + 1; 
  HBufC8 *buf8 = NULL; 
  buf8 = HBufC8::NewL(iLength);
  TPtr8 ptr8(buf8->Des());
  ptr8.Copy(aBytes);
  HBufC *buf = NULL;
  buf = HBufC::NewL(iLength);
  TPtr ptr = buf->Des();
  TRAPD(err,CnvUtfConverter::ConvertToUnicodeFromUtf8(ptr, ptr8));
  delete buf8;
    
     if (0 == iBuf.Compare(KMname_zh))
  {
  iXMLData->SetMname_zh(buf->Des());
  }
     else if (0 == iBuf.Compare(KMname_en))
  {
  iXMLData->SetMname_en(buf->Des());
  }
     else if (0 == iBuf.Compare(KZip))
  {
  iXMLData->SetZip(buf->Des());
  }
     else if (0 == iBuf.Compare(KPath))
  {
  iXMLData->SetPath(buf->Des());
  }
     else if (0 == iBuf.Compare(KJurnal))
  {
  iXMLData->SetJurnal(buf->Des());
  }
     else if (0 == iBuf.Compare(KImage))
  {
  iXMLData->SetImage(buf->Des());
  }
  else if (0 == iBuf.Compare(KDescription))
  {
  iXMLData->SetDescription(buf->Des());
  }
     else if (0 == iBuf.Compare(KMsize))
  {
  iXMLData->SetMsize(buf->Des());
  iXMLDataArray.Append(iXMLData);
   
  iXMLData = NULL;
  iXMLData = XML_Data::NewL();
  }
     else if(0 == iBuf.Compare(KSysText))
  {
   if(iSysnewText != NULL)
   {
    delete iSysnewText;
    iSysnewText = NULL;
   }
   iSysnewText = HBufC::NewL(buf->Length());
   iSysnewText->Des().Copy(buf->Des());
  }
      else if(0 == iBuf.Compare(KBigTypeName) || 0 == iBuf.Compare(KSmallTypeName))
         {
         iXMLDataType->SetTypeName(buf->Des());
         }
      else if(0 == iBuf.Compare(KBigTypeId) || 0 == iBuf.Compare(KSmallTypeId) )
          {
        iXMLDataType->SetTypeID(buf->Des());
     
        iXMLDataTypeArray.Append(iXMLDataType);      
        iXMLDataType = NULL;
        iXMLDataType = TXMLDataType::NewL();             
          }
  delete buf;
 }
 else
 {
 }
}
// --------------------------------------------------------------------------
void CXmlHandler::OnStartPrefixMappingL(const RString& /*aPrefix*/,
  const RString& /*aUri*/, TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  // Do something here...
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnEndPrefixMappingL(const RString& /*aPrefix*/,
  TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/,
  TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnSkippedEntityL(const RString& /*aName*/, TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  // Do something here...
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnProcessingInstructionL(const TDesC8& /*aTarget*/,
  const TDesC8& /*aData*/, TInt aErrorCode)
 {
 if (KErrNone == aErrorCode)
  {
  }
 else
  {
  }
 }
// --------------------------------------------------------------------------
void CXmlHandler::OnError(TInt aErrorCode)
 {
 _LIT( KErrorMessage, "*** OnError ***/f" );
 }
// --------------------------------------------------------------------------
TAny* CXmlHandler::GetExtendedInterface(const TInt32 /*aUid*/)
 {
 return 0;
 }

// End of File
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值