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?
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?
To parse an XML file the simpler Qt way is to use QXmlStreamReader. See Qt Docs for examples.
A camel can go 14 days without drink,
I can't!!!
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?
Search "QXmlStream Bookmarks Example" in Qt Assistant
A camel can go 14 days without drink,
I can't!!!
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??
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
You can use directly QXmlStreamReader in your code.
Simple code
Qt Code:
QXmlStreamReader xml(&file); while (!xml.atEnd()) { xml.readNext(); ... // do processing } if (xml.hasError()) { ... // do error handling } file.close();To copy to clipboard, switch view to plain text mode
A camel can go 14 days without drink,
I can't!!!
How can i reach the strings after reading?
I want to see, what it read.
So I will understand how this reader runs better.
This code may help u............
Qt Code:
#ifndef CONTACT_H #define CONTACT_H #include<QtCore/QString> class QDomElement; class QDomDocument; class QString; class Contact { public: QString name,eMail,phone; }; #endifTo copy to clipboard, switch view to plain text mode
ThanksQt Code:
//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> { name = iName; phone = iPhone; eMail = iEMail; } { cn.setAttribute( "NAME", c.name ); cn.setAttribute( "PHONE", c.phone ); cn.setAttribute( "EMAIL", c.eMail ); return cn; } int main( int argc, char **argv ) { doc.appendChild(root); Contact c; c.name = "AAAA"; c.eMail = "AAAA@gmail.com"; c.phone = "+91-32233223"; root.appendChild(ContactToNode(doc,c)); return -1; ts << doc.toString(); file.close(); c.name = "BBBBB"; c.eMail = "BBBB@gmail.com"; c.phone = "+91-3233443440"; root.appendChild( ContactToNode( doc, c ) ); return -1; ts << doc.toString(); c.name = "CCCCC"; c.eMail = "maassy@gmail.com"; c.phone = "+91-98232388"; root.appendChild( ContactToNode( doc, c ) ); return -1; ts << doc.toString(); file.close(); return -1; if( !doc.setContent( &file ) ) { file.close(); return -2; } root = doc.documentElement(); if( root.tagName() != "adbook" ) return -3; while( !n.isNull() ) { 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; }To copy to clipboard, switch view to plain text mode
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?
Last edited by bod; 26th June 2008 at 13:28.
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.
What is base class?
Qt Code:
QXmlStreamReader xml(&file); 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. while(!n.isNull()) { if(!e.isNull()) { ui.textEdit->setText("aGirdi"); ui.textEdit->setText( e.tagName()); // the node really is an element. } n = n.nextSibling(); }To copy to clipboard, switch view to plain text mode
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??
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?
A camel can go 14 days without drink,
I can't!!!
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
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.
Here is my code:Qt Code:
<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>To copy to clipboard, switch view to plain text mode
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 plsQt Code:
//QDomElement e5 = node.toElement(); //ui.textEdit->append("\nXxXxxX" + e5.tagName()); QDomNode locNode; for( locNode = node.firstChild(); (!locNode.isNull()); locNode = locNode.firstChild() ){ ui.textEdit->append("\n-_-_-" + e2.tagName()); if((locNode.firstChild()).isNull() ){ ui.textEdit->append("\nAAAAAAAAAAAAAAAAAAAAAAAA"); ui.textEdit->append("\n---??*?" + e3.tagName()); iconLocation =locElement.text(); locNode = locNode.nextSibling(); locElement = locNode.toElement(); iconLocation = iconLocation + "\\" + locElement.text(); ui.textEdit->append("\n+-+-+*" + iconLocation); } } }To copy to clipboard, switch view to plain text mode
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.....
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
Qt Code:
ui.textEdit->append("\n-_-_-" + e2.tagName()); if( e2.tagName() == "directory" ){To copy to clipboard, switch view to plain text mode
Bookmarks