×
Namespaces

Variants
Actions
(Difference between revisions)

Creating QDomDocument in Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan")
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
Line 38: Line 38:
 
==Header==
 
==Header==
  
<code cpp>
+
<code cpp-qt>
 
#include <QDomDocument>
 
#include <QDomDocument>
 
#include <QNetworkReply>
 
#include <QNetworkReply>
Line 48: 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 61: Line 61:
  
 
From HTTP reply {{Icode|QNetworkReply}}:
 
From HTTP reply {{Icode|QNetworkReply}}:
<code cpp>
+
<code cpp-qt>
 
QDomDocument* createDom(QNetworkReply* reply)
 
QDomDocument* createDom(QNetworkReply* reply)
 
{
 
{
Line 74: Line 74:
  
 
Example XML document:
 
Example XML document:
<code cpp>
+
<code cpp-qt>
 
<person>
 
<person>
 
     <id>1</id>
 
     <id>1</id>
Line 83: Line 83:
  
 
Getting data from {{Icode|QDomDocument}}:
 
Getting data from {{Icode|QDomDocument}}:
<code cpp>
+
<code cpp-qt>
 
QDomElement root = domDocument->documentElement();
 
QDomElement root = domDocument->documentElement();
  

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.
130 page views in the last 30 days.
×