PDA

View Full Version : xml file not getting update.



Niamita
8th September 2011, 11:11
Hi all
i am generating a .xml file , but when i am updating it , it is not getting update desired.
My code is


Condition_Frame *condition = new Condition_Frame();
Action_Frame *action = new Action_Frame();
if(id == 3)
{
QFile *file = new QFile("E:/new.txt");
QXmlStreamWriter *stream = new QXmlStreamWriter();
stream->setDevice(file);
stream->setAutoFormatting(true);
if(!file->exists())
{
file->open(QIODevice::WriteOnly);
stream->writeStartDocument("");
}
else
{
file->open(QIODevice::Append);
}
stream->writeStartElement("statement");
stream->writeStartElement("Condition");
stream->writeAttribute("id", QString::number(1));
stream->writeStartElement("input");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", condition->input_combobox->currentText());
stream->writeEndElement();
stream->writeStartElement("operator");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", condition->operator_combobox->currentText());
stream->writeEndElement();
stream->writeStartElement("values");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", condition->values_combobox->currentText());
stream->writeEndElement();
stream->writeStartElement("more");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", condition->more_combobox->currentText());
stream->writeEndElement();
stream->writeEndElement();
stream->writeStartElement("action");
stream->writeAttribute("id", QString::number(1));
stream->writeStartElement("output");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", action->output_combobox->currentText());
stream->writeEndElement();
stream->writeStartElement("operator");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", action->operator_combobox->currentText());
stream->writeEndElement();
stream->writeStartElement("values");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", action->values_combobox->currentText());
stream->writeEndElement();
stream->writeStartElement("more");
stream->writeAttribute("id", QString::number(1));
stream->writeTextElement("text", action->more_combobox->currentText());
stream->writeEndElement();
stream->writeEndDocument();
file->close();
}

Where condition_frame and Action_Frame are two different classes which contains comboboxes , from which value is getting and insert in xml file.
I am doing this in a slot. My problem is this when i first time click on button generated xml is right but when i click on button again , the data inserting in xml is same as the previous (not getting change when combobox values change).

Please guide me what i am doing wrong.

wysota
8th September 2011, 12:08
Your logic is probably flawed. Are you sure you want to append the data to the file if it exists? Even if you do, you should start with writeStartDocument(), the stream writer will not let you create a malformed xml document. But it is more likely you should overwrite the contents of the file with a document containing both the old content and the new one.

Niamita
8th September 2011, 12:16
ya i am also thinking that my logic is wrong and i start with writeStartDocument()(By mistake it is not written).
Actually the problem is that xml file contain the comboxes value that are at the object creation time , they are not getting changed in file when i change them.

wysota
8th September 2011, 13:15
The nature of xml and files is that you can't just add something in the middle, you need to replace the whole content or at least the part starting with the first insert into the file. And don't ignore return values of the methods you are using. Calling writeStartDocument() with an empty string is probably incorrect too. Either use the variant without parameters or pass "1.0".