Results 1 to 7 of 7

Thread: XML escape

  1. #1
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default XML escape

    Is there any function in QT to escape string for saving it in XML and decode it back after loading from XML ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML escape

    Could you give more details on your problem? Have you tried using a CDATA section?

  3. #3
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: XML escape

    I have user-provided string which I want to save into XML file and then read it again. In order to do it I should replace some characters (at least <>&, may be others?) by coresponging XML entities.

    Some strings are two short (object names) and there are too many of them to use CDATA. And even for CDATA I should somehow replace "]]>" sequence, shouldn't I ?

    May be the problem is not clearly Qt-related, but as Qt has XML parsers I've thought it can also has such a function.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML escape

    The simplest way is to base64 encode the string you want to store. Or you can do it properly by reencoding xml entities, but I think you'd have to do that manually.

  5. #5
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML escape

    I found 2 possible solutions:

    1) encode/decode the text with the static functions
    QUrl::toPercentEncoding()
    QUrl::fromPercentEncoding()
    You can specify what characters must be encoded and what not. You can simply make it encode "<", ">" and "&".
    Qt Code:
    1. // Original text
    2. QString text = "<text>";
    3. // Encode it
    4. QByteArray encoded = QUrl::toPercentEncoding("<text>", QByteArray(), "<>&");
    5. // --> encoded == "%3Ctext%3E"
    6. // Decode it
    7. QString decoded = QUrl::fromPercentEncoding(encoded);
    8. // --> decoded == "<text>"
    To copy to clipboard, switch view to plain text mode 

    2) use the new QXmlStreamWriter class which generates XML files, automatically encoding what is needed
    See QXmlStreamWriter::writeCharacters() and QXmlStreamWriter::writeCDATA().
    Last edited by iw2nhl; 10th July 2007 at 18:36.

  6. #6
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: XML escape

    2) use the new QXmlStreamWriter class which generates XML files, automatically encoding what is needed
    See QXmlStreamWriter::writeCharacters() and QXmlStreamWriter::writeCDATA().
    This seems to be exactly what I need ! Thanks a lot.

    Probably it was overlooked by me and others since it has appeared only in 4.3

  7. #7
    krsmichael Guest

    Default Re: XML escape

    You do not want to do what the others have suggested. Use these routines for your DomElement, ect values.

    Qt Code:
    1. QString EncodeXML
    2. (
    3. const QString& encodeMe
    4. )
    5. {
    6. QString temp;
    7.  
    8. for (quint32 index(0); index < encodeMe.size(); index++)
    9. {
    10. QChar character(encodeMe.at(index));
    11.  
    12. switch (character.unicode())
    13. {
    14. case '&':
    15. temp += "&amp;"; break;
    16.  
    17. case '\'':
    18. temp += "&apos;"; break;
    19.  
    20. case '"':
    21. temp += "&quot;"; break;
    22.  
    23. case '<':
    24. temp += "&lt;"; break;
    25.  
    26. case '>':
    27. temp += "&gt;"; break;
    28.  
    29. default:
    30. temp += character;
    31. break;
    32. }
    33. }
    34.  
    35. return temp;
    36. }
    37.  
    38. QString DecodeXML
    39. (
    40. const QString& decodeMe
    41. )
    42. {
    43. QString temp(decodeMe);
    44.  
    45. temp.replace("&amp;", "&");
    46. temp.replace("&apos;", "'");
    47. temp.replace("&quot;", "\"");
    48. temp.replace("&lt;", "<");
    49. temp.replace("&gt;", ">");
    50.  
    51. return temp;
    52. }
    To copy to clipboard, switch view to plain text mode 

    If you use toPercentEncoding or...base64 encoding, who can read the xml that you have created? One of the benefits of XML is being able to read it from a text editor. Don't give up that advantage.

  8. The following user says thank you to krsmichael for this useful post:

    hispeedsurfer (31st January 2022)

Similar Threads

  1. escape input for mysql server
    By tpf80 in forum Qt Programming
    Replies: 8
    Last Post: 18th June 2007, 22:58
  2. Escape Sequences in Qt
    By Kofie in forum Newbie
    Replies: 1
    Last Post: 21st March 2007, 10:33

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
  •  
Qt is a trademark of The Qt Company.