×
Namespaces

Variants
Actions

SAX parser comparison in Symbian and Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): All (S60)
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition, FP2
S60 5th Edition
S60 5th Edition
S60 3rd Edition FP2
S60 3rd Edition FP1
Article
Keywords: QXmlReader
Created: User:Kbwiki (23 Nov 2009)
Last edited: hamishwillee (30 May 2013)

Contents

Overview

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

Description

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

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

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

The reader reports parsing events through special handler classes:

QxmlContentHandler, QXmlDTDHandler, QXmlErrorHandler,
QXmlEntityResolver, QXmlDeclHandler, QXmlLexicalHandler.
The QXmlDefaultHandler 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 MContentHandler::OnStartDocumentL() XML reader calls QXmlContentHandler::startDocument(). Content handler can be set using QXmlReader::setContentHandler(QXmlContentHandler * handler).
Handling end of document MContentHandler::OnEndDocumentL() XML reader calls QXmlContentHandler::endDocument() after it has finished parsing.
Handling start of element MContentHandler::OnStartElementL() QXmlContentHandler::startElement()
Handling end of element MContentHandler::OnEndElementL() QXmlContentHandler::endElement()
Handling tag contents MContentHandler::OnContentL() QXmlContentHandler::characters()
Prefix-URI namespace mapping MContentHandler::OnStartPrefixMappingL() QXmlContentHandler::startPrefixMapping() and
QXmlContentHandler::endPrefixMapping()
Error handling MContentHandler::OnError() QXmlErrorHandler class provides an interface to report errors in XML data.

The error handler can be set with QXmlReader::setErrorHandler(). Errors can be reported using warning(), error(), and fatalError(), with the error text being reported with errorString().

Resolving external entities MContentHandler::OnSkippedEntityL() The QXmlEntityResolver class provides an interface to resolve external entities contained in XML data. Registering custom handlers for external entities is done with QXmlReader::setEntityResolver().

Sample Qt code for SAX parser

The following code creates a QTreeView 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 );
This page was last modified on 30 May 2013, at 09:34.
55 page views in the last 30 days.
×