PDA

View Full Version : XML parsing issue



yogesh
3rd March 2009, 06:47
Hi,
I have a requirement where I need to create an xml file programmatically. I went through the documentation of QDomDocument and got that it is possible thorugh creating a Document and then adding elements into it. Can we find any examples application in Qt on how to do it? I was able to do the reading of xml file now I want to write it also in a file.

Regards,
Yogesh

spirit
3rd March 2009, 06:54
there is an example in Qt Assistant


To create a document using DOM use code like this:
QDomDocument doc("MyML");
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);

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

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

QString xml = doc.toString();

yogesh
3rd March 2009, 07:26
Hi,
Thanks for the quick reply. I have tried that and it is creating an xml file now what I need is to create an xml file which has document type as
<?xml version="1.0" encoding="UTF-8"?>
How to achieve it ? As for using the above code it generates like
<!DOCTYPE MyML>
Please clarify this doubt.

Regards,
Yogesh

spirit
3rd March 2009, 07:34
take a look at this method


QDomProcessingInstruction QDomDocument::createProcessingInstruction ( const QString & target, const QString & data )

yogesh
3rd March 2009, 08:02
Hi,
I went through the documentation and it is still not clear for me as to how to prepare the XML declaration. What should I pass in "target" and "data" field so as to create <?xml version="1.0" encoding="UTF-8"?>?
Also the document mentioned something like
"The XML declaration that appears at the top of an XML document, typically <?xml version='1.0' encoding='UTF-8'?>, is treated by QDom as a processing instruction. This is unfortunate, since the XML declaration is not a processing instruction; among other differences, it cannot be inserted into a document anywhere but on the first line."
What does this imply that if I use this function it wont create a genuine xml which can be read by other tools?

Regards,
Yogesh

spirit
3rd March 2009, 08:05
try this


...
QDomProcessingInstruction process = document->createProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"utf-8\"");
document->appendChild(process);
...

talk2amulya
3rd March 2009, 09:08
u can also have a look at QXmlStreamWriter if u want a dedicated class for xml writing