SAX parser comparison in Symbian and Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

This article provides a comparison of XML document handling using Simple API for XML (SAX) parsers between Symbian/S60 and Qt.

Description[edit | edit source]

SAX is an event-based standard interface for XML parsers. Details on SAX2 can be found at http://www.saxproject.org.

Comparison of SAX (SAX 2.0) parsing in Symbian/S60 and Qt[edit | edit source]

Operation Symbian/S60 Qt
Main Class/Interface 76ytuiytuityutyutututyutyutyu interface. Also some classes extending the Symbian XML framework are provided by S60, mainly 76ytuiytuityutyutututyutyutyu and 76ytuiytuityutyutututyutyutyu. 76ytuiytuityutyutututyutyutyu interface (abstract class). Derived class 76ytuiytuityutyutututyutyutyu provides an implementation of a simple XML parser.
Event reporting 76ytuiytuityutyutututyutyutyu interface.

For extensions; through the 76ytuiytuityutyutututyutyutyu interface (not discussed here).

The reader reports parsing events through special handler classes:

76ytuiytuityutyutututyutyutyu, 76ytuiytuityutyutututyutyutyu, 76ytuiytuityutyutututyutyutyu,
76ytuiytuityutyutututyutyutyu, 76ytuiytuityutyutututyutyutyu, 76ytuiytuityutyutututyutyutyu.
The 76ytuiytuityutyutututyutyutyu class provides a default implementation of all the XML handler classes. This class gathers together the features of the above specialised handler classes.

Handling start of document 76ytuiytuityutyutututyutyutyu XML reader calls 76ytuiytuityutyutututyutyutyu. Content handler can be set using 76ytuiytuityutyutututyutyutyu.
Handling end of document 76ytuiytuityutyutututyutyutyu XML reader calls 76ytuiytuityutyutututyutyutyu after it has finished parsing.
Handling start of element 76ytuiytuityutyutututyutyutyu 76ytuiytuityutyutututyutyutyu
Handling end of element 76ytuiytuityutyutututyutyutyu 76ytuiytuityutyutututyutyutyu
Handling tag contents 76ytuiytuityutyutututyutyutyu 76ytuiytuityutyutututyutyutyu
Prefix-URI namespace mapping 76ytuiytuityutyutututyutyutyu 76ytuiytuityutyutututyutyutyu and
76ytuiytuityutyutututyutyutyu
Error handling 76ytuiytuityutyutututyutyutyu 76ytuiytuityutyutututyutyutyu class provides an interface to report errors in XML data.

The error handler can be set with 76ytuiytuityutyutututyutyutyu. Errors can be reported using 76ytuiytuityutyutututyutyutyu, 76ytuiytuityutyutututyutyutyu, and 76ytuiytuityutyutututyutyutyu, with the error text being reported with 76ytuiytuityutyutututyutyutyu.

Resolving external entities 76ytuiytuityutyutututyutyutyu The 76ytuiytuityutyutututyutyutyu class provides an interface to resolve external entities contained in XML data. Registering custom handlers for external entities is done with 76ytuiytuityutyutututyutyutyu.

Sample Qt code for SAX parser[edit | edit source]

The following code creates a 76ytuiytuityutyutututyutyutyu from an XML document showing all XML tags and contents.

 class SAXHandler: public QXmlDefaultHandler
   {      
 public: // Constructor and destructor
   SAXHandler( QTreeView* tree = 0 );
   virtual ~SAXHandler();
   
 public:   // From QXmlDefaultHandler
 
   bool startElement( const QString& namespaceURI, const QString& localName, 
                      const QString& qName, const QXmlAttributes& atts );
                      
   bool endElement( const QString& namespaceURI, const QString& localName, 
                    const QString& qName );
                    
   bool characters( const QString& ch );
   
   bool endDocument();
   
   bool fatalError( const QXmlParseException& exception );
   
 private:
   
   QStandardItemModel*   m_model;         // Model of the tree to be filled
   QStandardItem*        m_currentItem;   // Tag currently being parsed
   QString m_currentText;   // Current text of the tag
   QTreeView*            m_tree;          // Tree view to be filled
   };
 
 
 SAXHandler::SAXHandler( QTreeView* tree )
   {
   m_tree = atree;
   m_model = new QStandardItemModel;
   }
 
 
 bool SAXHandler::startElement( const QString & /* namespaceURI */,
                                const QString & /* localName */,
                                const QString &qName,
                                const QXmlAttributes &attributes )
   {
   // Create a standard item with the qualified name and append it to model
   m_currentItem =  new QStandardItem( qName );
   m_model->appendRow( m_currentItem );
   // List all the attributes
   QStringList attrList;
   for ( int i = 0; i < attributes.count(); i++ ) {
     attrList.append( attributes.value( i ) );
     }
   return true;
   }
   
   
 bool SAXHandler::endElement( const QString & /* namespaceURI */,
                              const QString & /* localName */,
                              const QString &qName )
   {
   // Appending the row with text from current tag
   m_currentItem->appendRow( new QStandardItem( m_currentText ) );
   m_currentText.clear();
   return true;
   }
   
   
 bool SAXHandler::characters( const QString &str )
   {
   m_currentText.append( str );
   return true;
   }
   
     
 bool SAXHandler::endDocument()
   {
   // Model is now fully constructed with tags and content
   m_tree->setModel( m_model );
   }
 
 
 // XML reader uses this function to report a non-recoverable error 
 // Details of the error are stored in QXmlParseException
 bool SAXHandler::fatalError( const QXmlParseException &exception )
   {
   // column number and line where the error ocurred
   int col = exception.columnNumber();
   int line = exception.lineNumber();
   // error message
   QString msg = exception.message();
   return false;
   }
   
   
 SAXHandler::~SAXHandler()
   {
   }


Setting an input source, content and error handlers for the XML reader

 QFile file( myXMLFile );
 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
   // Handle file opening error
   }
 QXmlInputSource* xmlInput = new QXmlInputSource( &file );
 // Contruct a custom QXmlDefaultHandler-derived handler
 SAXHandler* handler = new SAXHandler( m_tree );
 
 // Use our own class as both content and error handler
 // m_reader is of type QXmlSimpleReader 
 m_reader.setContentHandler( handler );
 m_reader.setErrorHandler( handler );
 m_reader.parse( xmlInput );