Results 1 to 6 of 6

Thread: Extracting xml fragment from QXmlStreamReader

  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Extracting xml fragment from QXmlStreamReader

    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.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Extracting xml fragment from QXmlStreamReader

    No other solution is it ?
    Ok atleast i'll remove this question from unanswered list by posting now
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting xml fragment from QXmlStreamReader

    Quote Originally Posted by Gopala Krishna View Post
    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?

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Extracting xml fragment from QXmlStreamReader

    Quote Originally Posted by jacek View Post
    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 ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Extracting xml fragment from QXmlStreamReader

    Quote Originally Posted by Gopala Krishna View Post
    Is suggestions only acceptable from customers ?
    No, anyone can send both bug reports and suggestions.
    J-P Nurmi

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Extracting xml fragment from QXmlStreamReader

    Good that i haven't yet suggested because i found a solution for this.. Yippe!!

    Here is the code for anyone interested..
    Qt Code:
    1. QString readXmlFragment(QXmlStreamReader *reader)
    2. {
    3. // Make sure reader is at starting element
    4. Q_ASSERT(reader->isStartElement());
    5.  
    6. //The string to be returned containing xml fragment.
    7. QString retVal;
    8.  
    9. QXmlStreamWriter writer(&retVal);
    10. writer.setCodec("UTF-8");
    11.  
    12. //write the current token which is start element.
    13. writer.writeCurrentToken(*reader);
    14.  
    15. //Store the tag name of start element.
    16. QString startName = reader->name().toString();
    17.  
    18. // Now keep writing all tokens till end tag corresponding to startName is obtained.
    19. while(!reader->atEnd()) {
    20. reader->readNext();
    21. if(reader->isEndElement() && reader->name() == startName)
    22. break;
    23. writer.writeCurrentToken(*reader);
    24. }
    25. //write the end tag now
    26. writer.writeCurrentToken(*reader);
    27.  
    28. //return the fragment
    29. return retVal;
    30. }
    To copy to clipboard, switch view to plain text mode 

    There seems to be one small quirk though. The empty elements of type
    Qt Code:
    1. <sometag a="A" />
    To copy to clipboard, switch view to plain text mode 
    will be written as
    Qt Code:
    1. <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
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.