PDA

View Full Version : XML Data displayed in one file



mourad
4th July 2012, 17:53
Hello everbody,
I'm développed an application which extract data from a database and save them in an XML file.
Bonjour tout le monde, j'ai développé une application qui extrait des données depuis une base de données puis insère ces données dans un fichier XML à l'aide de QXmlStreamWriter.

/*open a file */
QFile file(QString("%1\\specialite.xml").arg(qApp->applicationDirPath()));

if (!file.open(QIODevice::OnlyWrite | QIODevice::Text))
{
/* show wrror message if not able to open file */
QMessageBox::warning(0, "Read only", "The file is in read only mode");
}
else
{
QTextStream out(&file);
out.setCodec("UTF-8");

int index = 1;

/*if file is successfully opened then create XML*/
QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();

/* set device (here file)to streamwriter */
xmlWriter->setDevice(&file);

/* Writes a document start with the XML version number version. */
xmlWriter->writeStartDocument();

/* Creer le noeud principal de toutes les spécialites*/
xmlWriter->writeStartElement("Specialites");

/*
--- Parcourcourir la requete et creer un noeud pour chaque spécialite
--- Mettre les informations de la spécailité
*/
while (speQuery.next())
{
// Creation du noeud de la spécialité
xmlWriter->writeStartElement("Specialite");

/* ajouter attribut ID SPE*/
xmlWriter->writeTextElement("Id", speQuery.value(0).toString());

/* ajouter attribut Nom spe*/
xmlWriter->writeTextElement("Nom", speQuery.value(1).toString());

/* ajouter attibut Nom Long*/
xmlWriter->writeTextElement("NomLong", speQuery.value(2).toString());
}
The problem is that all data is displayed in one line in the file.
Does anyone have an idea about this problem and can help me to resolve it.
Many thanks in adavance.
Best regards.

Lesiok
4th July 2012, 18:08
Did You read QXmlStreamWriter doc ? I think : NO. Look at QXmlStreamWriter::autoFormatting (http://qt-project.org/doc/qt-4.8/qxmlstreamwriter.html#autoFormatting-prop)

mourad
4th July 2012, 18:28
Thanks a lot for usefull response. It works fine.
In fact, in the QXmlStreamWriter doc, it is indicated that the setAutoFormatting function was introduced in Qt 4.4 and I'm using 4.3 sothat i did'nt test it :o
Thank for your help