View Full Version : Extracting xml fragment from QXmlStreamReader
Gopala Krishna
25th November 2007, 17:05
I recently started using xml in my app and i fell prey to QXmlStream* classes.
So far i found it really good and easy, but i came across a situation where i had to extract a fragment of xml from a xml document.
To be precise i want to extract svg embedded in xml so that i can construct QGraphicsSvgItems.
Is there a way to do this using QXmlStreamReader class ?
Currently i use dom to get the fragment but this is tedious as well as double work.
I am also open to suggestion regarding any special kind of embedding svg to enable it to be extracted .
P.S: Opening an external svg is already possible, i just need to add an option for embedded svgs.
Gopala Krishna
29th November 2007, 18:12
No other solution is it ?
Ok atleast i'll remove this question from unanswered list by posting now ;)
jacek
29th November 2007, 18:41
Ok atleast i'll remove this question from unanswered list by posting now ;)
Hey, that's cheating! ;)
Maybe you should send a suggestion to the Trolls that such functionality might be useful?
Gopala Krishna
30th November 2007, 08:06
Hey, that's cheating! ;)
Maybe you should send a suggestion to the Trolls that such functionality might be useful?
Ok, i tried to suggest but i can only report bug in task-tracker. Is suggestions only acceptable from customers ?
jpn
30th November 2007, 09:37
Is suggestions only acceptable from customers ?
No, anyone can send both bug reports and suggestions. :)
Gopala Krishna
1st December 2007, 09:14
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.
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;
}
There seems to be one small quirk though. The empty elements of type
<sometag a="A" /> will be written as
<sometag a="A"></sometag>
which should not be a problem in 99.99% of cases ;)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.