PDA

View Full Version : XML in Qt



qwakaw
10th July 2008, 13:42
Hii,

I would like to know/have a sample code for inserting, deleting, updating nodes in an XML file.
I tried with the QXmlStreamReader, QXmlStreamWriter but could get it done.

Thank you,

JimDaniel
10th July 2008, 16:02
For all my xml stuff I use QDomDocument,QDomNode and QDomElement. They have a pretty simple interface for doing what you require. The documentation in the links has examples for you.

qwakaw
14th July 2008, 06:48
i have used QDomDocument to insert node at the end but resulting output is not as desired.

I am sending a sample code


existing XML file ---- made use of appendChild and insertAfter

mydocument.xml

<!DOCTYPE mydocument>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>


mydocument.xml // generated xml file

<!DOCTYPE mydocument>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>



// code that generates the above xml file
QDomDocument doc;

QFile file("mydocument.xml");
if(!file.open(QIODevice::ReadWrite))
{
qWarning("Open --- readwrite --err");
exit (0);
}
//used for reading
if(!doc.setContent(&file))
{
qWarning("setcontent --err");
file.close();
exit (0);
}

QDomElement root = doc.documentElement();

QDomElement cn = doc.createElement("contact");

cn.setAttribute("name","Sita");
cn.setAttribute("phone", "1234567");
cn.setAttribute("address","ayodyanagari");

root.appendChild(cn);
QTextStream ts(&file);
root.save(ts,QDomNode::EncodingFromTextStream);


//generated file
mydocument.xml


<!DOCTYPE mydocument>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />


// code that generates the above xml file


QDomNode newnode=doc.insertAfter(cn, doc.lastChild());
QTextStream ts(&file);
newnode.save(ts,QDomNode::EncodingFromTextStream);

yxmaomao
14th July 2008, 07:23
you should write into a new file or clear old file contents then write

wysota
14th July 2008, 07:33
And save the whole document and not only the root node - otherwise you could be missing some xml declarations.

qwakaw
14th July 2008, 08:02
is there no other way to insert new node in an existing xml file, other than creating new xml file.

wysota
14th July 2008, 10:16
The question should be: "is there no way of inserting data in the middle of an existing file" and the answer is "No, you have to recreate/rewrite the file". It's just a matter of opening the file in a different mode, you know.

qwakaw
25th July 2008, 14:13
i got a solution but it's a 3rd party support---- called libxml2 using this as well as with the GLIB support i am able to do the task... any way thanks for all the response.. Qt Centre rocks .. but guys please and if possible try to give away some code snippets.... that would be helpful for others too..

wysota
25th July 2008, 14:33
libxml overwrites the file as well, trust me :) If you want to overwrite a file, simply open the file in overwrite mode and save your contents as usual.