PDA

View Full Version : DomDocument to XML: producing empty files



athomas
9th May 2012, 13:17
Hello,
I am trying to take a QDomDocument and write to a .xml. I have got so far but now I can only produce empty documents.



QDomDocument doc("MyML");
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);

QDomElement tag = doc.createElement("greetings");
root.appendChild(tag);

QDomText t = doc.createTextNode("Hello World");
tag.appendChild(t);

QString xml = doc.toString();
cout << qPrintable(xml) << endl;


QFile file("something.xml");
if (!file.open(QIODevice::WriteOnly))
{
QTextStream TextStream(&file);
TextStream << xml;
file.close();
}

return 0;

and this produces


<!DOCTYPE MyML>
<MyML>
<greetings>Hello World</greetings>
</MyML>
any help would be much appreciated,
Thanks,
a.

Le_B
9th May 2012, 16:06
the test of your open is wrong

athomas
10th May 2012, 13:17
the test of your open is wrong

not particularly helpful... any more to add? something like doc.setContent? I haven't looked at it in a couple of days. I'll try again tomorrow I think.

athomas
10th May 2012, 15:40
----------FIXED-------------

Fixed the problem, really silly mistake.


QFile file("something.xml");
if (file.open(QIODevice::WriteOnly))
{
QTextStream TextStream(&file);
TextStream << xml;
file.close();
}

return 0;

Changed the condition in the if loop. Removed not operator, duh! Works perfectly now.

Le_B
11th May 2012, 10:11
that's what i was saying :)