problem in reading xml file with SAX parser.
Hi all
I am reading a xml file but i am not successing in reading it. My code for reading file is
Code:
{
tablewidget = table;
table_item = 0;
}
{
if (qName == "statement"){
tablewidget->insertRow(1);
if(qName == "Condition")
{
QString str_id
= attributes.
value("text");
qDebug()<<str_id;
}
else if(qName == "action")
{
QString str_id
= attributes.
value("text");
qDebug()<<str_id;
}
else if(qName == "text")
currenttext.clear();
}
return true;
}
bool Xmlhandler
::characters(const QString &str
) {
currenttext += str;
return true;
}
{
if(qName == "statement"){
if (qName == "Condition") {
if (qName == "page") {
table_item->setText(currenttext);
tablewidget->setItem(0, 0, table_item);
}
}
if (qName == "action") {
if (qName == "page") {
table_item->setText(currenttext);
tablewidget->setItem(0, 1, table_item);
}
}
}
return true;
}
{
"%2:\n%3.").arg(exception.lineNumber()).arg(exception.columnNumber()).arg(exception.message()));
return false;
}
and my xml file is like this-
<?xml version="1.0" encoding="UTF-8"?>
<statement>
<Condition id="1">
<text></text>
</Condition>
<action id="1">
<text></text>
</action>
</statement>
<statement>
<Condition id="2">
<text>Analog Input Equals to 1 END</text>
</Condition>
<action id="2">
<text>Digital Output Equals to ON END</text>
</action>
</statement>
i am trying to read xml and display it in a table.
Please guide me , i am doing this since three days but i am not getting desired result.
Thank you.
Re: problem in reading xml file with SAX parser.
What is wrong exactly? Saying "it doesn't work" doesn't provide any hints as to what is wrong. One thing might be that you don't have a root element in your xml.
Re: problem in reading xml file with SAX parser.
ya i also think so , so that i am now generating xml with rrot element through DOM parser, but when i create file then it is ok but when i want to add new entries it is not getting append to the root node in xml file.
My code for this is
Code:
const int Indent = 4;
root = doc.createElement("root");
if(!file->exists())
{
doc.appendChild(root);
}
else
{
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
());
root.appendChild(statement);
statement.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
}
if(file->error())
qDebug()<<file->errorString();
doc.save(out, Indent);
what i am doing wrong now.
Re: problem in reading xml file with SAX parser.
You can't append to the file like that. You need to replace the contents of the file and not append to it.
Re: problem in reading xml file with SAX parser.
Please be more elaborated, it is possible to give some sample code.
Re: problem in reading xml file with SAX parser.
If you have a document and you modify it, you need to save the whole document to the file overwriting the old version of the document. XML doesn't work in an "additive" manner. It's a matter of understanding how XML works. If you open the file in append mode, you will get two documents one after the other instead of having the updated document only.
Re: problem in reading xml file with SAX parser.
So what i have to do now , do i have to create temproryfile.
Re: problem in reading xml file with SAX parser.
You have to read the original document from the file, update it with new entries and save it back again. No temporary files are needed.
Re: problem in reading xml file with SAX parser.
Can you please give me some sample code. I have confused a lot.
Re: problem in reading xml file with SAX parser.
Code:
file.
open(QFile::ReadWrite);
doc.setContent(&file);
root.appendChild(newOne);
file.seek(0);
doc.save(stream); // or file.write(doc.toByteArray());
Re: problem in reading xml file with SAX parser.
I tried as you suggested but now i am facing a problem when i close the program and then write on the file , it write nothing on the file but it is working fine when the program is running once. I am getting error that "Calling appendChild() on a null node does nothing. " what is wrong with this.
My code is:
Code:
const int Indent = 4;
root = doc.createElement("root");
if(!file->exists())
{
doc.appendChild(root);
}
else
{
file
->open
(QFile::ReadWrite);
root1 = doc.documentElement();
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.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
root1.appendChild(statement);
file->seek(0);
}
doc.save(out, Indent)
It is solved. Actually i am not setting contents of document.
doc.setContent(file);
Re: problem in reading xml file with SAX parser.
Hi
How to update a textnode data in xml through QDomElement.
I have following code
Code:
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();
int a = string_data.toInt();
qDebug()<<string_data;
string_data.clear();
id_text = doc.createTextNode("Qt"); // Here i want to write Qt but it is getting write 0Qt on the textnode.
node.appendChild(id_text);
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", i);
statement.appendChild(condition_node);
statement.appendChild(action_node);
condition_node.appendChild(condition_text);
action_node.appendChild(action_text);
root1.appendChild(statement);
// QString str = statement.attribute("id");
// qDebug()<<str.toInt();
file->seek(0);
}
doc.save(out, Indent);
So i want to update the textnode value while it is getting append in previous value.
Please anyone help me.
Waiting for response.