×
Namespaces

Variants
Actions
(Difference between revisions)

Creating QDomDocument in Qt

From Nokia Developer Wiki
Jump to: navigation, search
copyeditor (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{KBCS}}
+
[[Category:Qt]]
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=CS001430
+
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices=Nokia 5800 XpressMusic
+
|devices= Nokia 5800 XpressMusic
|category=Qt for S60
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=
+
|platform= S60 3rd Edition, FP1, FP2<br>S60 5th Edition
|creationdate=June 16, 2009
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=QDomDocument, QDomElement
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= QDomDocument, QDomElement
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090603
 +
|author= [[User:Tepaa]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory=
 +
|id= CS001430
 
}}
 
}}
  
 
==Overview==
 
==Overview==
  
This code snippet demonstrates how to use <tt>QDomDocument</tt> to represent an XML document.
+
This code snippet demonstrates how to use {{Icode|QDomDocument}} to represent an XML document.
 
+
'''Note''': In order to use this code, you need to have Qt for S60 installed on your platform.
+
  
  
 
==Preconditions==
 
==Preconditions==
  
* Install the Qt for S60 Garden release from here: [http://pepper.troll.no/s60prereleases/ Qt for S60 "Garden" pre-release]
+
* Install latest [[Qt SDK]]
* Check the installation guide: [http://pepper.troll.no/s60prereleases/doc/install-s60.html How to install the package]
+
  
  
 
==Header==
 
==Header==
  
<code cpp>
+
<code cpp-qt>
 
#include <QDomDocument>
 
#include <QDomDocument>
 
#include <QNetworkReply>
 
#include <QNetworkReply>
Line 35: Line 48:
  
 
From file:
 
From file:
<code cpp>
+
<code cpp-qt>
 
QDomDocument doc("mydocument");
 
QDomDocument doc("mydocument");
 
QFile file("mydocument.xml");
 
QFile file("mydocument.xml");
Line 47: Line 60:
 
</code>
 
</code>
  
From HTTP reply <tt>QNetworkReply</tt>:
+
From HTTP reply {{Icode|QNetworkReply}}:
<code cpp>
+
<code cpp-qt>
QNetworkReply* createDom(QNetworkReply* reply)
+
QDomDocument* createDom(QNetworkReply* reply)
    {
+
{
 
     QDomDocument* doc = new QDomDocument();
 
     QDomDocument* doc = new QDomDocument();
 
     doc->setContent(reply);
 
     doc->setContent(reply);
 
     return doc;
 
     return doc;
    }
+
}
 
</code>
 
</code>
  
Line 61: Line 74:
  
 
Example XML document:
 
Example XML document:
<code cpp>
+
<code cpp-qt>
 
<person>
 
<person>
 
     <id>1</id>
 
     <id>1</id>
Line 69: Line 82:
 
</code>
 
</code>
  
Getting data from <tt>QDomDocument</tt>:
+
Getting data from {{Icode|QDomDocument}}:
<code cpp>
+
<code cpp-qt>
 
QDomElement root = domDocument->documentElement();
 
QDomElement root = domDocument->documentElement();
  
Line 85: Line 98:
 
==See also==
 
==See also==
  
* For more information about <tt>QDomDocument</tt>, see [http://doc.qtsoftware.com/4.5/qdomdocument.html QDomDocument Class Reference].
+
* For more information about {{Icode|QDomDocument}}, see [http://doc.qt.nokia.com/4.5/qdomdocument.html QDomDocument Class Reference].
  
  
Line 92: Line 105:
  
  
[[Category:Qt]][[Category:Qt for S60]][[Category:Code Examples]]
+
[[Category:Code Snippet]][[Category:Code Snippet]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:16, 11 October 2012

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QDomDocument, QDomElement
Created: tepaa (03 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Overview

This code snippet demonstrates how to use QDomDocument to represent an XML document.


[edit] Preconditions


[edit] Header

#include <QDomDocument>
#include <QNetworkReply>
#include <QFile>


[edit] Create QDomDocument

From file:

QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();

From HTTP reply QNetworkReply:

QDomDocument* createDom(QNetworkReply* reply)
{
QDomDocument* doc = new QDomDocument();
doc->setContent(reply);
return doc;
}


[edit] Getting data from QDomDocument

Example XML document:

<person>
<id>1</id>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>

Getting data from QDomDocument:

QDomElement root = domDocument->documentElement();
 
if (root.tagName() == "person")
{
qDebug() << root.firstChildElement("id").text();
qDebug() << root.firstChildElement("firstname").text();
qDebug() << root.firstChildElement("lastname").text();
// TODO: Store data, this code only debugs data into log
}


[edit] See also


[edit] Postconditions

QDomDocument is created and data is parsed.

This page was last modified on 11 October 2012, at 04:16.
105 page views in the last 30 days.