Questo e' il file da leggere
----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<frane>
<frana id="1">
<localita>Pino Torinese</localita>
<comune>Torino</comune>
<data>19/01/2016</data>
<ora>1:45 pm</ora>
<compilatore>Luca Innocenti</compilatore>
<annotazioni></annotazioni>
<est></est>
<nord></nord>
<sistema>WGS84</sistema>
<A>4</A>
<EE>1</EE>
<H>2</H>
<ALPHA>3</ALPHA>
<F>2</F>
<V>4</V>
<M>2</M>
<codice>3/<codice>
</frana>
</frane>
----------------------------
e questo e' lo scheletro del codice Qt (piuttosto autoesplicativo). Prima si carica il file come xmlBOM e poi lo di scorre cercando le varie chiavi
Qt = + xml
----------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QtXml>
#include <QFile>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QDomDocument xmlBOM;
QFile f("/Users/lucainnocenti/Triage.xml");
if (!f.open(QIODevice::ReadOnly ))
{
std::cerr << "Error while loading file" << std::endl;
}
xmlBOM.setContent(&f);
f.close();
QDomElement root=xmlBOM.documentElement();
QDomElement Component=root.firstChild().toElement();
while(!Component.isNull())
{
if (Component.tagName()=="frana")
{
QDomElement Child=Component.firstChild().toElement();
QString localita,comune,data,ora,compilatore;
int A;
while (!Child.isNull())
{
// Read Name and value
if (Child.tagName()=="localita") localita=Child.firstChild().toText().data();
if (Child.tagName()=="comune") comune=Child.firstChild().toText().data();
if (Child.tagName()=="data") data=Child.firstChild().toText().data();
if (Child.tagName()=="ora") ora=Child.firstChild().toText().data();
if (Child.tagName()=="compilatore") compilatore=Child.firstChild().toText().data();
if (Child.tagName()=="A") A=Child.firstChild().toText().data().toInt();
Child = Child.nextSibling().toElement();
}
std::cout << "Localita = " << localita.toStdString().c_str() << std::endl;
std::cout << "Comune = " << comune.toStdString().c_str() << std::endl;
std::cout << "Data = " << data.toStdString().c_str() << std::endl;
std::cout << "Ora = " << ora.toStdString().c_str() << std::endl;
std::cout << "Compilatore = " << compilatore.toStdString().c_str() << std::endl;
std::cout << "A = " << localita.toStdString().c_str() << std::endl;
std::cout << std::endl;
ui->label->setText(comune);
}
Component = Component.nextSibling().toElement();
}
}
----------------------------
Nessun commento:
Posta un commento