PDA

View Full Version : Creating new XML file



Pembar
15th May 2009, 12:13
Hey guys, I have the following code:



void xmlBaseTest::myBlankFile()
{
QFile data("booklist.xml");
QDomElement root = doc->createElement("booklist");
doc->appendChild(root);

if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
doc->save(out,4);
}
}


It creates the file:


<!DOCTYPE serverlist>
<booklist/>


The line <booklist/> is causing problems, when trying to read it later using .setContent. I was wondering if there was any way to get it to print out <booklist></booklist> instead?

Thanks much

wysota
15th May 2009, 23:29
It can't be causing problems, it's perfectly valid. What exactly are the "problems"?

auba
16th May 2009, 01:26
I have an other question about "valid XML":
is it possible to create a header line
<?xml version=\"1.0\" encoding=\"iso8859-1\" ?>"
by some calls of whatever or do I have to set them with


DomDocumentType doctype = impl.createDocumentType("name", QString::null, QString::null);
QDomDocument doc = impl.createDocument("uri", "name", doctype);
doc.setContent(QString("<?xml version=\"1.0\" encoding=\"iso8859-1\" ?>"));


Sorry, this piece of code is from Qt3. Until now I did not face the differences of XML code in Qt4.

Lykurg
16th May 2009, 09:06
<?xml version=\"1.0\" encoding=\"iso8859-1\" ?>"

QDomDocument doc("RootElement");
QDomProcessingInstruction process = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"iso8859-1\"");
doc.appendChild(process);