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>
<!DOCTYPE mydocument>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>
To copy to clipboard, switch view to plain text mode
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>
<!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>
To copy to clipboard, switch view to plain text mode
// code that generates the above xml file
QFile file("mydocument.xml");
{
qWarning("Open --- readwrite --err");
exit (0);
}
//used for reading
if(!doc.setContent(&file))
{
qWarning("setcontent --err");
file.close();
exit (0);
}
cn.setAttribute("name","Sita");
cn.setAttribute("phone", "1234567");
cn.setAttribute("address","ayodyanagari");
root.appendChild(cn);
root.
save(ts,
QDomNode::EncodingFromTextStream);
// 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);
To copy to clipboard, switch view to plain text mode
//generated file
mydocument.xml
<!DOCTYPE mydocument>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
<!DOCTYPE mydocument>
<root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
</root-element>
<contact phone="1234567" address="ayodyanagari" name="Sita" />
To copy to clipboard, switch view to plain text mode
// code that generates the above xml file
QDomNode newnode
=doc.
insertAfter(cn, doc.
lastChild());
newnode.
save(ts,
QDomNode::EncodingFromTextStream);
QDomNode newnode=doc.insertAfter(cn, doc.lastChild());
QTextStream ts(&file);
newnode.save(ts,QDomNode::EncodingFromTextStream);
To copy to clipboard, switch view to plain text mode
Bookmarks