PDA

View Full Version : help in reading XML file



cshiva_in
24th March 2008, 12:40
Hi All

I am just a beginner of Qt. So Pls excuse me if I have made some silly errors. I was trying to parse a simple XML file and wanted to show it on a simple interface. I have got the code here...

test.cpp


#include<qfile.h>
#include<qdom.h>
#include<iostream.h>

QFile file("simple.xml");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug("Failed to open file for reading");
return -1;
}

QDomDocument document;
if(!document.setContent(&file))
{
qDebug("Failed to parse the file into a DOM tree");
file.close();
return -1;
}

file.close();

QDomElement documentElement = document.documentElement();
QDomNode node = document.firstChild();
while(!node.isNull())
{
if(node.isElement())
{
QDomElement element = node.toElement();
cout<<"Element"<<element.tagName();
cout<<"Element attribute name"<<element.attribute("name", "not set")
}


if (node.isText())
{
QDomText text = node.toText();
qDebug << text.data();
}

node=node.nextSibling();
}


This is the error which I got.


test.cpp:6: error: expected unqualified-id before ‘if’
test.cpp:13: error: expected unqualified-id before ‘if’
test.cpp:20: error: expected constructor, destructor, or type conversion before ‘.’ token
test.cpp:24: error: expected unqualified-id before ‘while’
make: *** [test.o] Error 1

and here is the XML file


<document name="DocName">
<author name="AuthorName" />
</document>

If anyone could help it would be absolute gr8!!!!

Cheers
Shiva

yogeshm02
24th March 2008, 13:55
I'm not in mood to get into details ;) but I think all this is supposed to be part of a function



QFile file("simple.xml");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug("Failed to open file for reading");
return -1;
}

QDomDocument document;
if(!document.setContent(&file))
{
qDebug("Failed to parse the file into a DOM tree");
file.close();
return -1;
}

file.close();

QDomElement documentElement = document.documentElement();
QDomNode node = document.firstChild();
while(!node.isNull())
{
if(node.isElement())
{
QDomElement element = node.toElement();
cout<<"Element"<<element.tagName();
cout<<"Element attribute name"<<element.attribute("name", "not set")
}


if (node.isText())
{
QDomText text = node.toText();
qDebug << text.data();
}

node=node.nextSibling();
}