Good that i haven't yet suggested because i found a solution for this.. Yippe!!
Here is the code for anyone interested..
QString readXmlFragment
(QXmlStreamReader
*reader
) {
// Make sure reader is at starting element
Q_ASSERT(reader->isStartElement());
//The string to be returned containing xml fragment.
QXmlStreamWriter writer(&retVal);
writer.setCodec("UTF-8");
//write the current token which is start element.
writer.writeCurrentToken(*reader);
//Store the tag name of start element.
QString startName
= reader
->name
().
toString();
// Now keep writing all tokens till end tag corresponding to startName is obtained.
while(!reader->atEnd()) {
reader->readNext();
if(reader->isEndElement() && reader->name() == startName)
break;
writer.writeCurrentToken(*reader);
}
//write the end tag now
writer.writeCurrentToken(*reader);
//return the fragment
return retVal;
}
QString readXmlFragment(QXmlStreamReader *reader)
{
// Make sure reader is at starting element
Q_ASSERT(reader->isStartElement());
//The string to be returned containing xml fragment.
QString retVal;
QXmlStreamWriter writer(&retVal);
writer.setCodec("UTF-8");
//write the current token which is start element.
writer.writeCurrentToken(*reader);
//Store the tag name of start element.
QString startName = reader->name().toString();
// Now keep writing all tokens till end tag corresponding to startName is obtained.
while(!reader->atEnd()) {
reader->readNext();
if(reader->isEndElement() && reader->name() == startName)
break;
writer.writeCurrentToken(*reader);
}
//write the end tag now
writer.writeCurrentToken(*reader);
//return the fragment
return retVal;
}
To copy to clipboard, switch view to plain text mode
There seems to be one small quirk though. The empty elements of type
<sometag a="A" />
<sometag a="A" />
To copy to clipboard, switch view to plain text mode
will be written as
<sometag a="A"></sometag>
<sometag a="A"></sometag>
To copy to clipboard, switch view to plain text mode
which should not be a problem in 99.99% of cases
Bookmarks