DOM parser comparison in Symbian/S60 and Qt
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.
Article Metadata
Tested with
Compatibility
Article
Contents |
Overview
This article provides a comparison of Document Object Model (DOM) parser in Symbian and Qt.
Description
DOM provides an interface to access and change the content and structure of an XML file. It makes a hierarchical (tree) view of the document. Thus, in contrast to the SAX2 interface, an object model of the document is resident in memory after parsing which makes manipulation easy. For more information, visit http://www.w3.org/DOM/
Comparison of DOM parsing in Symbian and Qt
| Operation | Symbian/S60 5th Edition | Qt on Symbian |
| Library/Module | XmlEngineDom.lib | QtXml Module |
| DOM Document | RXmlEngDocument | QDomDocument |
| Creating (parsing)a DOM tree | RXmlEngDOMParser::ParseFileL() | QDomDocument::setContent() |
| Extracting root (document) element | RXmlEngDocument::DocumentElement() | QDomDocument::documentElement() |
| DOM elements | TXmlEngElement | QDomElement |
| Traversing nodes | TXmlEngNode::FirstChild() to get the first child (if any), TXmlEngNode::NextSibling() to traverse.
Also see LastChild(), <tt>PreviousSibling() and ParentNode(). To find a node with a particular name, use Name(). |
QDomNode::firstChild() to get the first child (if any), and QDomNode::nextSibling() to traverse. Also see lastChild(), previousSibling() and parentNode(). To find a node with a particular name, use namedItem(). |
| Extracting tag name | When TXmlEngNode::NodeType() is EElement, TXmlEngNode::Name() returns the tag name. | QDomElement::tagName() returns the tag name for an element. |
| Extracting attributes | Instance of the TXmlEngAttr class represents an XML attribute. TXmlEngAttr::Value() returns the attribute value.
TXmlEngElement::AttributeValueL() returns a value of an attribute with given name and namespace URI. |
Element attributes are represented by QDomAttr objects, queried using QDomElement::attribute() and QDomElement::attributeNode().
QDomAttr::value() returns the attribute value. QDomAttr::name() returns the attribute name. |
| Extracting text of the element | TXmlEngElement::Text() returns the content of the element.
TXmlEngTextNode interface inherits from TXmlEngCharacterData and represents the textual content ("character data" in XML) of an element or attribute. TXmlEngCharacterData::Contents() returns the content of the node. |
QDomElement::text() returns text for the element.
To find all text in all of node's children, iterate over the children looking for QDomText nodes and get the text using QDomText::data(). |
| XML comments | TXmlEngComment | QDomComment |
| XML processing instructions | TXmlEngProcessingInstruction | QDomProcessingInstruction |
| CDATA sections | TXmlEngCDATASection | QDomCDATASection |
Sample Qt code for DOM parser
The following code snippet creates a tree view from an XML document with all the tags, content, and attributes.
QDOMSimple::QDOMSimple( QWidget *parent )
: QWidget( parent )
{
// DOM document
QDomDocument doc( "title" );
// Opening the XML file to be parsed
QFile file( XMLFILENAME );
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
// The tree view to be filled with xml data
// (m_tree is a class member variable)
m_tree = new QTreeWidget( this );
// Creating the DOM tree
doc.setContent( &file );
file.close();
// Root of the document
QDomElement root = doc.documentElement();
// Taking the first child node of the root
QDomNode child = root.firstChild();
// Setting the root as the header of the tree
QTreeWidgetItem* header = new QTreeWidgetItem;
header->setText( 0, root.nodeName() );
m_tree->setHeaderItem( header );
// Parse until the end of document
while ( !child.isNull() ) {
//Convert a DOM node to DOM element
QDomElement element = child.toElement();
//Parse only if the node is a really an element
if ( !element.isNull() ) {
//Parse the element recursively
parseElement( element );
//Go to the next sibling
child = child.nextSiblingElement();
}
}
m_tree->setGeometry( QApplication::desktop()->availableGeometry() );
setGeometry( QApplication::desktop()->availableGeometry() );
}
}
void QDOMSimple::parseElement( QDomElement& aElement, QTreeWidgetItem* aParentItem )
{
// A map of all attributes of an element
QDomNamedNodeMap attrMap = aElement.attributes();
// List all attributes
QStringList attrList;
for ( int i = 0; i < attrMap.count(); i++ ) {
// Attribute name
QString attr = attrMap.item( i ).nodeName();
attr.append( "-" );
// Attribute value
attr.append( attrMap.item( i ).nodeValue() );
attrList.append( attr );
}
QTreeWidgetItem* item;
// Create a new view item for elements having child nodes
if ( aParentItem ) {
item = new QTreeWidgetItem( aParentItem );
}
// Create a new view item for elements without child nodes
else {
item = new QTreeWidgetItem( m_tree );
}
//Set tag name and the text
QString tagNText;
tagNText.append( aElement.tagName() );
tagNText.append( "-" );
tagNText.append( aElement.text() );
item->setText( 0, tagNText );
// Append attributes to the element node of the tree
for ( int i = 0; i < attrList.count(); i++ ) {
QTreeWidgetItem* attrItem = new QTreeWidgetItem( item );
attrItem->setText( 0, attrList[i] );
}
// Repeat the process recursively for child elements
QDomElement child = aElement.firstChildElement();
while ( !child.isNull() ) {
parseElement( child, item );
child = child.nextSiblingElement();
}
}


(no comments yet)