void HomeWindow::create_xml_file()
{
if(!edit_flag){ /// craeting the file and write in it in this condition
const int Indent = 4;
int id_value;
root = doc.createElement("root");
if(!file->exists())
{
id_text = doc.createTextNode("0");
root.appendChild(id);
id.appendChild(id_text);
doc.appendChild(root);
}
else
{
file
->open
(QFile::ReadWrite);
doc.setContent(file);
root1 = doc.documentElement();
id_value = id_string.toInt();
new_id_node.appendChild(new_id_Text); // replace existing node with new node
root1.replaceChild(new_id_node, node);
statement = doc.createElement("statement");
condition_node = doc.createElement("condition");
action_node = doc.createElement("action");
QDomText condition_text
= doc.
createTextNode(condition_text_edit
->toPlainText
());
QDomText action_text
= doc.
createTextNode(action_text_edit
->toPlainText
());
statement.setAttribute("id", id_value);
statement.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
root1.appendChild(statement);
file->seek(0);
static int i =1;
table->setRowCount(i);
int row = i;
row = --row;
table->setItem(row, 0, item_id);
table->setItem(row, 1, item_condition);
table->setItem(row, 2, item_action);
i++;
condition_text_edit->clear();
action_text_edit->clear();
}
doc.save(out, Indent);
if(file->error())
qDebug()<<"error"<<file->errorString();
}
else{ ///////edit the file
const int indent =8;
file
->open
(QFile::ReadWrite);
doc.setContent(file);
while(!node.isNull())
{
if (node.toElement().tagName() == "statement"){
if(string_id == node.toElement().attribute("id"))
{
QDomElement statement
= doc.
createElement("statement");
statement.setAttribute("id", string_id);
QDomElement condition_node
= doc.
createElement("condition");
QDomText condition_text
= doc.
createTextNode(condition_text_edit
->toPlainText
());
QDomText action_text
= doc.
createTextNode(action_text_edit
->toPlainText
());
statement.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
root_edit.replaceChild(statement, node.toElement());
break;
}
}
node = node.nextSibling();
}
doc.save(out, indent);
table->setItem(row_index, 0, item_id);
table->setItem(row_index, 1, item_condition);
table->setItem(row_index, 2, item_action);
condition_text_edit->clear();
action_text_edit->clear();
edit_flag = false;
}
}
void HomeWindow::create_xml_file()
{
QFile *file = new QFile("E:/new.txt");
if(!edit_flag){ /// craeting the file and write in it in this condition
const int Indent = 4;
int id_value;
static QDomDocument doc;
static QDomElement root,root1;
QDomElement statement, action_node, condition_node;
QDomText id_text;
root = doc.createElement("root");
if(!file->exists())
{
file->open(QIODevice::WriteOnly);
QDomElement id = doc.createElement("id");
id_text = doc.createTextNode("0");
root.appendChild(id);
id.appendChild(id_text);
doc.appendChild(root);
}
else
{
file->open(QFile::ReadWrite);
doc.setContent(file);
root1 = doc.documentElement();
QDomElement node = root1.firstChildElement("id");
QString id_string = node.text();
id_value = id_string.toInt();
QDomElement new_id_node = doc.createElement(QString("id"));
QDomText new_id_Text = doc.createTextNode(QString::number(++id_value));
new_id_node.appendChild(new_id_Text); // replace existing node with new node
root1.replaceChild(new_id_node, node);
statement = doc.createElement("statement");
condition_node = doc.createElement("condition");
action_node = doc.createElement("action");
QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
statement.setAttribute("id", id_value);
statement.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
root1.appendChild(statement);
file->seek(0);
static int i =1;
table->setRowCount(i);
QTableWidgetItem *item_id = new QTableWidgetItem(QString::number(id_value));
QTableWidgetItem *item_condition = new QTableWidgetItem(condition_text_edit->toPlainText());
QTableWidgetItem *item_action = new QTableWidgetItem(action_text_edit->toPlainText());
int row = i;
row = --row;
table->setItem(row, 0, item_id);
table->setItem(row, 1, item_condition);
table->setItem(row, 2, item_action);
i++;
condition_text_edit->clear();
action_text_edit->clear();
}
QTextStream out(file);
doc.save(out, Indent);
if(file->error())
qDebug()<<"error"<<file->errorString();
}
else{ ///////edit the file
const int indent =8;
QDomDocument doc;
file->open(QFile::ReadWrite);
doc.setContent(file);
QDomElement root_edit = doc.documentElement();
QDomNode node = root_edit.firstChild();
while(!node.isNull())
{
if (node.toElement().tagName() == "statement"){
if(string_id == node.toElement().attribute("id"))
{
QDomElement statement = doc.createElement("statement");
statement.setAttribute("id", string_id);
QDomElement condition_node = doc.createElement("condition");
QDomElement action_node = doc.createElement("action");
QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
statement.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
root_edit.replaceChild(statement, node.toElement());
break;
}
}
node = node.nextSibling();
}
QTextStream out(file);
doc.save(out, indent);
QTableWidgetItem *item_id = new QTableWidgetItem(string_id);
QTableWidgetItem *item_condition = new QTableWidgetItem(condition_text_edit->toPlainText());
QTableWidgetItem *item_action = new QTableWidgetItem(action_text_edit->toPlainText());
table->setItem(row_index, 0, item_id);
table->setItem(row_index, 1, item_condition);
table->setItem(row_index, 2, item_action);
condition_text_edit->clear();
action_text_edit->clear();
edit_flag = false;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks