PDA

View Full Version : How to read a simple xml file.



bod
26th June 2008, 10:43
Hi,
First of all I must say "I am new to qt programming and also xml".
I need to parse an xml file and read the file names. That's all I want to do.
But I couldn't find any code example of reading simply an xml file. Anybody can help me please?

mcosta
26th June 2008, 10:52
To parse an XML file the simpler Qt way is to use QXmlStreamReader. See Qt Docs for examples.

bod
26th June 2008, 11:06
What do you mean by Qt docs.
I looked at Qt assistant and looked at QXmlStreamReader also. But these donot include a sample code.
Could you give me a link please?

mcosta
26th June 2008, 11:09
Search "QXmlStream Bookmarks Example" in Qt Assistant

bod
26th June 2008, 11:12
I looked at that example. Do I need to implement those xbelhandler classes?
Do I need to write classses to read just a simple file??

aamer4yu
26th June 2008, 11:16
Just have a look at QDomDocument, QDomElement ., and QDomElement::text(). and QDomDocument::setContent. This might be sufficient for u :)

u can also refer to simple DOM Model in Qt Demos

mcosta
26th June 2008, 11:29
You can use directly QXmlStreamReader in your code.

Simple code


QFile file(fileName);
file.open(QIODevice::ReadOnly);

QXmlStreamReader xml(&file);
while (!xml.atEnd())
{
xml.readNext();
... // do processing
}
if (xml.hasError())
{
... // do error handling
}
file.close();

bod
26th June 2008, 12:59
How can i reach the strings after reading?
I want to see, what it read.
So I will understand how this reader runs better.

coderCPP1981
26th June 2008, 13:32
This code may help u............


#ifndef CONTACT_H
#define CONTACT_H

#include<QtCore/QString>

class QDomElement;
class QDomDocument;
class QString;

class Contact
{
public:
QString name,eMail,phone;

Contact( QString iName = "", QString iPhone = "", QString iEMail = "" );
Contact( const QDomElement &e );

};

#endif






//contact.cpp

#include"contact.h"

#include <qapplication.h>
#include <QtXml/QDomNode>

#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
#include <QDebug>
#include <QtXml/QDomDocument>




Contact::Contact( QString iName, QString iPhone, QString iEMail )
{
name = iName;
phone = iPhone;
eMail = iEMail;
}

QDomElement ContactToNode(QDomDocument &d, const Contact &c )
{
QDomElement cn = d.createElement( "contact" );

cn.setAttribute( "NAME", c.name );
cn.setAttribute( "PHONE", c.phone );
cn.setAttribute( "EMAIL", c.eMail );

return cn;
}


int main( int argc, char **argv )
{

QApplication a( argc, argv );
QDomDocument doc("AdBookML");
QDomElement root = doc.createElement( "adbook" );
doc.appendChild(root);

Contact c;

c.name = "AAAA";
c.eMail = "AAAA@gmail.com";
c.phone = "+91-32233223";

root.appendChild(ContactToNode(doc,c));

QFile file("test.xml");
if( !file.open( QIODevice::WriteOnly ) )
return -1;

QTextStream ts( &file );
ts << doc.toString();

file.close();


c.name = "BBBBB";
c.eMail = "BBBB@gmail.com";
c.phone = "+91-3233443440";

root.appendChild( ContactToNode( doc, c ) );
if( !file.open( QIODevice::WriteOnly ) )
return -1;


ts << doc.toString();

c.name = "CCCCC";
c.eMail = "maassy@gmail.com";
c.phone = "+91-98232388";

root.appendChild( ContactToNode( doc, c ) );
if( !file.open( QIODevice::WriteOnly ) )
return -1;


ts << doc.toString();

file.close();

if( !file.open(QIODevice::ReadOnly ))
return -1;
if( !doc.setContent( &file ) )
{
file.close();
return -2;
}
root = doc.documentElement();
if( root.tagName() != "adbook" )
return -3;
QDomNode n = root.firstChild();
while( !n.isNull() )
{
QDomElement e = n.toElement();
if( !e.isNull() )
{
if( e.tagName() == "contact" )
{
Contact c ;

c.name = e.attribute( "NAME","" );
c.phone = e.attribute( "PHONE","" );
c.eMail = e.attribute( "EMAIL","" );

qDebug()<<c.name;
qDebug()<<c.phone;
qDebug()<<c.eMail;

}
}

n = n.nextSibling();
}
return 0;
}





Thanks

bod
26th June 2008, 13:45
If I didnot understand wrong, the code above first create an xml file then reads it.
Am I right?

Edit: And also what is the difference between QDomElement and QDomNode?
What are they beneficial For?

cyberboy
26th June 2008, 14:31
The QDomElement class represents one element in the DOM tree and the QDomNode class is the base class for all the nodes in a DOM tree.

bod
26th June 2008, 14:39
What is base class?

bod
26th June 2008, 14:43
QFile file("xmldenemev2.xbel");
QXmlStreamReader xml(&file);
QDomDocument doc("mydocument");

if (!file.open(QIODevice::ReadOnly))
return;

if (!(doc.setContent(&file))) {
ui.textEdit->setText("Fail in set Content");
file.close();
return;
}
file.close();

ui.textEdit->setText("SetContent is ok");
// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();

QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
ui.textEdit->setText("aGirdi");
ui.textEdit->setText( e.tagName()); // the node really is an element.
}
n = n.nextSibling();
}

Here is my code. But there is a mistake.It shouldn't enter the second "if". But it does.why "doc.setContent(&file)" returns false??

mcosta
26th June 2008, 15:14
As Qt Documentation says QDomDocument::setContent returns FALSE when the file isn't successfully parsed.

Probably there is an error in XML file. Can you post it?

bod
26th June 2008, 16:00
Yes, I noticed that there is an error in the xml file. I have started to read, and playing with it. Thank you very much everybody who tries to help me

bod
27th June 2008, 08:57
Hi, I started to read the xml file but now, I got an error.

Here is my xml file.Im trying to read the file and compute the icons' address.



<Icons>
<backIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>back.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>back.png</name>
</belongsTo>
</backIcon>

<nextIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>forward.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>Next.png</name>
</belongsTo>
</nextIcon>

<stopIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>stop.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>delete.png</name>
</belongsTo>
</stopIcon>

<saveIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>save.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>save.png</name>
</belongsTo>
</saveIcon>

<printIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>print.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>Print.png</name>
</belongsTo>
</printIcon>

<mailIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>mail.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>Email.png</name>
</belongsTo>
</mailIcon>

<standartViewIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>View.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>normalView.png</name>
</belongsTo>
</standartViewIcon>

<zoomInIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>zoomin.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>zoomin.png</name>
</belongsTo>
</zoomInIcon>

<zoomOutIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>zoomout.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>zoomout.png</name>
</belongsTo>
</zoomOutIcon>

<exitIcon>
<belongsTo iconset="Vista">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\VistaIconSet</directory>
<name>exit.png</name>
</belongsTo>
<belongsTo iconset="Classic">
<directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\ Images\ClassicIconSet</directory>
<name>exit.png</name>
</belongsTo>
</exitIcon>
</Icons>
Here is my code:

for(QDomNode node=e.firstChild(); (!node.isNull()); node=node.nextSibling()){
//QDomElement e5 = node.toElement();
//ui.textEdit->append("\nXxXxxX" + e5.tagName());
QDomNode locNode;
for( locNode = node.firstChild(); (!locNode.isNull()); locNode = locNode.firstChild() ){
QDomElement e2 = locNode.toElement();
ui.textEdit->append("\n-_-_-" + e2.tagName());
if((locNode.firstChild()).isNull() ){
ui.textEdit->append("\nAAAAAAAAAAAAAAAAAAAAAAAA");
QDomElement e3 = locNode.toElement();
ui.textEdit->append("\n---??*?" + e3.tagName());
QDomElement locElement = locNode.toElement();
iconLocation =locElement.text();
locNode = locNode.nextSibling();
locElement = locNode.toElement();
iconLocation = iconLocation + "\\" + locElement.text();
ui.textEdit->append("\n+-+-+*" + iconLocation);
}
}
}
With the outer for loop I visit every icon. And With the Inner one, I'm trying to compute "directory\filename". However it does not enter the if part. When the reader reads the node <directory>, it should enter. Because that node has no child.Also it enters the inner loop 3 times, however it should enter 2?. Anybody can help me pls

coderCPP1981
27th June 2008, 09:39
In your xml file there are different tags related to icons .
Do you want to read address /file name of each tag ?

Plz explain more.....

bod
27th June 2008, 10:13
there are 10 icons in my toolbar, backIcon, nextIcon etc..
in directory tag, i store the information about where is the directory path.
in name tag, i store the file name.
So "directory"\"filename" gives me the complete path.(like "C:\Documents and Settings\mydocs\nextIcon.png".
I dont know why the code above didnot work. but i did it with changing


QDomElement e2 = locNode.toElement();
ui.textEdit->append("\n-_-_-" + e2.tagName());
if( e2.tagName() == "directory" ){