PDA

View Full Version : Getting Data from XML File and placing them in lineEdits



zellwwf
15th August 2011, 22:07
Ok, heres my code

<Item_stats>
<General_Details>
<Price>300</Price>
<Name>Bottle of Wisdom</Name>
<Description> blahblahblah blahblah</Description>
</General_Details>

<Auras>
<HPreg>3.6</HPreg>
<Manareg>3.6</Manareg>
</Auras>
</Item_stats>

that is my basic xml file, i tried using #include QXml and using QdomDocument ... didnt reach a thing.. although i managed to have the file in 'open' state, figured that out using if statement with a file.isOpen...

Ok, what i want to do with the data is to place them into little Labels/Line edits. for example, i want this to be visible in the gui:

Item Name: (text here)
Price: 300...
if you can give me an example on one i would manage the rest.

Thanks in advance... XD
:cool:

wysota
15th August 2011, 23:00
didnt reach a thing..
Could you translate it to English, please? Or at least something more verbose, we've run out of crystal balls.

ChrisW67
15th August 2011, 23:04
QXmlSimpleReader or QXmlStreamReader would be good places to start.

marcvanriet
15th August 2011, 23:14
Hi,

Use QXmlStreamReader. Just some code snippets to get you going :



QFile your_text_file = open the text file first
QXmlStreamReader stream( your_text_file );
pXml = &stream;

while( !pXML->atEnd() )
{
pXML->readNext();

if( pXML->isStartElement() )
{
if( pXML->name()=="Item_stats") // found start of my data
{
while( !pXML->atEnd() )
{
pXML->readNext();

if( pXML->isEndElement() )
{
if( pXML->name() == "Item_stats" )
break; // end of my data
}
else if( pXML->isStartElement() ) // entry like 'Auras'
{
// do something with pXML->name()
// and with pXML->readElementText()
// and maybe pXML->attributes().value( "yourattribute")
}
}
}
}
}

zellwwf
16th August 2011, 00:05
Hi,

Use QXmlStreamReader. Just some code snippets to get you going :



QFile your_text_file = open the text file first
QXmlStreamReader stream( your_text_file );
pXml = &stream;

while( !pXML->atEnd() )
{
pXML->readNext();

if( pXML->isStartElement() )
{
if( pXML->name()=="Item_stats") // found start of my data
{
while( !pXML->atEnd() )
{
pXML->readNext();

if( pXML->isEndElement() )
{
if( pXML->name() == "Item_stats" )
break; // end of my data
}
else if( pXML->isStartElement() ) // entry like 'Auras'
{
// do something with pXML->name()
// and with pXML->readElementText()
// and maybe pXML->attributes().value( "yourattribute")
}
}
}
}
}


Thank you... thank you :D ... i will post my results with this tomorrow morning.. i am extremely sleepy, i have been up all day working on this project (not only this part)... With extreme thanks :D

upon reading the quote, why do we need a text file? and are we just placing the data from the xml to the text file then reading it?
thanks

upon reading the quote, why do we need a text file? and are we just placing the data from the xml to the text file then reading it?
thanks

ChrisW67
16th August 2011, 01:17
If you have the XML data in a file (free standing or in a resource) then that file is the text file. If you already have the XML data in-memory, i.e. in a QByteArray, then you can use QBuffer in place of QFile as the QIODevice for the stream reader.

zellwwf
16th August 2011, 02:01
thank you again... if i run into trouble again i will inform you

zellwwf
16th August 2011, 12:01
I have a little error that i ran into, pXml, what kind of variable is it
i get this error "pXml was not declared in this scope"
The Code is in a header just because i don't like messing around with my main cpp, i try to always to put stuff into headers... i know the code isn't complete, but i wanted to test somethings first


//
//#INCLUDES
#ifndef XMLSTUFF_H
#define XMLSTUFF_H
#include <qstring>
#include <QXmlStreamReader>
#include <Qfile>
#include <QMessageBox> //for debugging purposes
//END INCLUDES

static void readXml() {
QMessageBox mbx;
QFile myFile("/xml/champs/ashe_prototype.xml"); //this is the path in my resources
myFile.open(QFile::ReadOnly);
if (myFile.isOpen()) {

//MESSAGE BOXES
mbx.setText(" It is Open");
mbx.exec();

//STREAMERS XML
QXmlStreamReader stream( &myFile );

pXml = &stream;

//WHILE LOOPS
while (!pXml->atEnd()){
pXml->readNext();
mbx.setText=pXml.name();
mbx.exec();

}


} else {
mbx.setText(" !It is Open");
mbx.exec();
}
}



#endif // XMLSTUFF_H

zellwwf
16th August 2011, 15:12
sorry to bother you again, but why am i getting pXML is not declared in this scope

stampede
16th August 2011, 15:29
Looks like it should be QXmlStreamReader *, but I have no clue what for (you can just use stream).

QFile myFile("/xml/champs/ashe_prototype.xml"); //this is the path in my resources
If you are trying to access resource file, then it should be more like

QFile myFile(":/xml/champs/ashe_prototype.xml"); // with :/ at the beggining

zellwwf
16th August 2011, 20:06
Ok... sweet, i managed to read what i want from the xml.. now, the problem is assigning the data obtain to QObjects/Widgets

#include "lolapp.h"
#include "ui_lolapp.h"
#include "xmlstuff.h"
#include <QtXml>

//General Variables ************************************************** *********************************
static void readXml();
bool inDetails,inSkills,inPassive,inQ,inW,inE,inR,inHea lth,inMana,inEnergy,inPhys,inMag,inDef,inExtra;
//************************************************** ************************************************** **

LOLApp::LOLApp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LOLApp)
{
ui->setupUi(this);

//initialize booleans for xml reading//
inDetails=inSkills=inPassive=inQ=inW=inE=inR=inHea lth=inMana=inEnergy=inPhys=inMag=inDef=inExtra=fal se;
//***********************************

readXml();
}

LOLApp::~LOLApp()
{
delete ui;
}
//FUNCTION DECLARATION
static void readXml() {
QMessageBox mbx;
QFile myFile(":/xml/champs/ashe_prototype.xml");
QXmlStreamReader stream( &myFile );
myFile.open(QFile::ReadOnly);

if (myFile.isOpen()) {
//MESSAGE BOXES********************************************* *
mbx.setText(" It is Open XD");
mbx.setStandardButtons(QMessageBox::Abort | QMessageBox::Ok);
mbx.exec();
//WHILE LOOPS********************************************* **
while (!stream.atEnd()){
stream.readNext();
if (stream.isStartElement()) {
if (stream.name()=="Details") {
if (stream.name()=="Name"){
//should add a code to add it to the costume label Champ_Name_Label.
//now here, i am supposed to link the name "Akali" to Champ_Name_Label
//which is located in the form (ui) of this cpp... eg; this is called lolapp.ui
//i tried LOLApp-> LOLApp:: LOLApp. nothing seems to work, i tried it alot...
//i just need to know how to put akali, string, into Champ_Name_Label. thanks

}
}
}
}


} else {
mbx.setText(" It is NOT Open");
mbx.exec();
}
}


Added after 4 minutes:

Ok... sweet, i managed to read what i want from the xml.. now, the problem is assigning the data obtain to QObjects/Widgets

#include "lolapp.h"
#include "ui_lolapp.h"
#include "xmlstuff.h"
#include <QtXml>

//General Variables ************************************************** *********************************
static void readXml();
bool inDetails,inSkills,inPassive,inQ,inW,inE,inR,inHea lth,inMana,inEnergy,inPhys,inMag,inDef,inExtra;
//************************************************** ************************************************** **

LOLApp::LOLApp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LOLApp)
{
ui->setupUi(this);

//initialize booleans for xml reading//
inDetails=inSkills=inPassive=inQ=inW=inE=inR=inHea lth=inMana=inEnergy=inPhys=inMag=inDef=inExtra=fal se;
//***********************************

readXml();
}

LOLApp::~LOLApp()
{
delete ui;
}
//FUNCTION DECLARATION
static void readXml() {
QMessageBox mbx;
QFile myFile(":/xml/champs/ashe_prototype.xml");
QXmlStreamReader stream( &myFile );
myFile.open(QFile::ReadOnly);

if (myFile.isOpen()) {
//MESSAGE BOXES********************************************* *
mbx.setText(" It is Open XD");
mbx.setStandardButtons(QMessageBox::Abort | QMessageBox::Ok);
mbx.exec();
//WHILE LOOPS********************************************* **
while (!stream.atEnd()){
stream.readNext();
if (stream.isStartElement()) {
if (stream.name()=="Details") {
if (stream.name()=="Name"){
//should add a code to add it to the costume label Champ_Name_Label.
//now here, i am supposed to link the name "Akali" to Champ_Name_Label
//which is located in the form (ui) of this cpp... eg; this is called lolapp.ui
//i tried LOLApp-> LOLApp:: LOLApp. nothing seems to work, i tried it alot...
//i just need to know how to put akali, string, into Champ_Name_Label. thanks

}
}
}
}


} else {
mbx.setText(" It is NOT Open");
mbx.exec();
}
}

stampede
16th August 2011, 20:48
Make the "readXml" method a (non-static and not const) member of LOLApp class, then you will have access to ui form (via this->ui->something).

zellwwf
16th August 2011, 20:56
i removed static, and i added the following


this->ui->champ_name_label.setText("dkdkd");//resulted in "invalid use of this in non-member function
// when using the folloing:
ui->...// reulsted in ui was not declared in this scope


FULL CODE NOW:

#include "lolapp.h"
#include "ui_lolapp.h"
#include "xmlstuff.h"
#include <QtXml>

//General Variables ************************************************** *********************************
void readXml();
bool inDetails,inSkills,inPassive,inQ,inW,inE,inR,inHea lth,inMana,inEnergy,inPhys,inMag,inDef,inExtra;
//************************************************** ************************************************** **

LOLApp::LOLApp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LOLApp)
{
ui->setupUi(this);

//initialize booleans for xml reading//
inDetails=inSkills=inPassive=inQ=inW=inE=inR=inHea lth=inMana=inEnergy=inPhys=inMag=inDef=inExtra=fal se;
//***********************************

readXml();
}

LOLApp::~LOLApp()
{
delete ui;
}
//FUNCTION DECLARATION
void readXml() {
QMessageBox mbx;
QFile myFile(":/xml/champs/ashe_prototype.xml");
QXmlStreamReader stream( &myFile );
myFile.open(QFile::ReadOnly);

if (myFile.isOpen()) {
//MESSAGE BOXES********************************************* *
mbx.setText(" It is Open");
mbx.setStandardButtons(QMessageBox::Abort | QMessageBox::Ok);
mbx.exec();
//WHILE LOOPS********************************************* **
while (!stream.atEnd()){
stream.readNext();
if (stream.isStartElement()) {
if (stream.name()=="Details") {
if (stream.name()=="Name"){
//should add a code to add it to the costume label Champ_Name_Label.
ui->champ_name_label.setText("asf");

}
}
}
}


} else {
mbx.setText(" !It is Open");
mbx.exec();
}
}

stampede
16th August 2011, 21:01
i removed static, and ...
Make this method a non-static member of a class.

zellwwf
16th August 2011, 21:07
i am an idiot, but how do i do that? should i create a class for it? can u provide an example:(?

stampede
16th August 2011, 21:18
should i create a class for it?
You already have a class, what I mean is to add a method to it.

can u provide an example?
Sure, there is a nice set of examples by Bruce Eckel, read here (http://www.smart2help.com/e-books/ticpp-2nd-ed-vol-one/Frames.html).