PDA

View Full Version : How to get a whole node of Xml as text



rawfool
13th August 2014, 12:05
For example if I have a xml like this -



<?xml version="1.0" encoding="UTF-8"?>
<tree path="//" message="SUCCESS">
<item restype="0" resname="1myd" size="-" ver="-"
lmd="2014/03/12 02:29:09" thumb="N" attrib_star="0"
attrib_desc="0" share="-" capture_date="0"
save_time="2014/03/12 02:29:09" chk="NA"/>
<item restype="0" resname="Test333" size="-" ver="-"
lmd="2014/03/11 00:53:42" thumb="N" attrib_star="0"
attrib_desc="0" share="-" capture_date="0"
save_time="2014/03/11 00:53:42" chk="NA"/>
</tree>



Now I need the whole node as string like this-


Output:
<item restype="0" resname="Test333" size="-" ver="-" lmd="2014/03/11 00:53:42" thumb="N" attrib_star="0" attrib_desc="0" share="-" capture_date="0" save_time="2014/03/11 00:53:42" chk="NA"/>


What I tried was not giving me this whole thing as text.


void Util::parseXml(void)
{
QDomDocument domDoc("myDoc");
QFile file("/home/rahul/Desktop/file.xml");
if(!file.open(QIODevice::ReadOnly))
return;
if (!domDoc.setContent(&file))
{
file.close();
return;
}
file.close();

QDomNode node = domDoc.elementsByTagName("item").at(0);
QDomElement elem = node.toElement();
qDebug() << elem.text();
}

Kindly help me with this. Thank you.

anda_skoa
13th August 2014, 12:23
See QDomNode::save() or http://qt-project.org/doc/qt-4.8/qdomnode.html#save

Cheers,
_

rawfool
18th August 2014, 12:22
Hi anda_skoa,

How can I get the same thing (one full node as QString) using QXmlStreamReader ?

Thank you.

anda_skoa
18th August 2014, 17:48
Hmm.

QXmlStreamReader::readNext() returns an indicator on what the stream is currently at, so it should be possible to "replay" the same things on a QXmlStreamWriter.
E.g. when the token is StartElement, write a start element and so on.

Cheers,
_