PDA

View Full Version : problem in reading xml file with SAX parser.



Niamita
13th September 2011, 07:05
Hi all

I am reading a xml file but i am not successing in reading it. My code for reading file is


Xmlhandler::Xmlhandler(QTableWidget *table)
{
tablewidget = table;
table_item = 0;
}

bool Xmlhandler::startElement(const QString &, const QString &, const QString &qName,const QXmlAttributes &attributes)
{
if (qName == "statement"){
tablewidget->insertRow(1);

if(qName == "Condition")
{
QString str_id = attributes.value("text");
qDebug()<<str_id;
table_item = new QTableWidgetItem();

}
else if(qName == "action")
{
QString str_id = attributes.value("text");
qDebug()<<str_id;
table_item = new QTableWidgetItem();

}
else if(qName == "text")
currenttext.clear();
}
return true;
}

bool Xmlhandler::characters(const QString &str)
{
currenttext += str;
return true;
}

bool Xmlhandler::endElement(const QString &,const QString &,const QString &qName)
{
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;
}

bool Xmlhandler::fatalError(const QXmlParseException &exception)
{
QMessageBox::warning(0, QObject::tr("SAX Handler"),QObject::tr("Parse error at line %1, column "
"%2:\n%3.").arg(exception.lineNumber()).arg(exception.column Number()).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.

wysota
13th September 2011, 09:24
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.

Niamita
13th September 2011, 12:33
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


const int Indent = 4;
static QDomDocument doc;
static QDomElement root;
QDomElement statement, action_node, condition_node;
root = doc.createElement("root");
QFile *file = new QFile("E:/new.txt");
if(!file->exists())
{
file->open(QIODevice::WriteOnly);
doc.appendChild(root);
}
else
{
file->open(QIODevice::Append);
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();

QTextStream out(file);
doc.save(out, Indent);

what i am doing wrong now.

wysota
13th September 2011, 12:43
You can't append to the file like that. You need to replace the contents of the file and not append to it.

Niamita
13th September 2011, 13:02
Please be more elaborated, it is possible to give some sample code.

wysota
13th September 2011, 13:06
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.

Niamita
13th September 2011, 13:10
So what i have to do now , do i have to create temproryfile.

wysota
13th September 2011, 13:16
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.

Niamita
13th September 2011, 13:24
Can you please give me some sample code. I have confused a lot.

wysota
13th September 2011, 13:32
QFile file("some.xml");
file.open(QFile::ReadWrite);
QDomDocument doc;
doc.setContent(&file);
QDomElement root = doc.documentElement();
QDomElement newOne = root.createElement("elem");
root.appendChild(newOne);
file.seek(0);
QTextStream stream(&file);
doc.save(stream); // or file.write(doc.toByteArray());

Niamita
14th September 2011, 05:58
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:


const int Indent = 4;
static QDomDocument doc;
static QDomElement root,root1;
QDomElement statement, action_node, condition_node;
root = doc.createElement("root");
QFile *file = new QFile("E:/new.txt");
if(!file->exists())
{
file->open(QIODevice::WriteOnly);
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);
}
QTextStream out(file);
doc.save(out, Indent)

It is solved. Actually i am not setting contents of document.
doc.setContent(file);

Niamita
14th September 2011, 10:13
Hi

How to update a textnode data in xml through QDomElement.
I have following code


static QDomDocument doc;
static QDomElement root,root1;
QDomElement statement, action_node, condition_node;
QDomText id_text;
root = doc.createElement("root");
QFile *file = new QFile("E:/new.txt");
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 string_data = node.text();
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);

}
QTextStream out(file);
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.