PDA

View Full Version : XML escape



Vladimir
8th July 2007, 14:54
Is there any function in QT to escape string for saving it in XML and decode it back after loading from XML ?

wysota
9th July 2007, 15:36
Could you give more details on your problem? Have you tried using a CDATA section?

Vladimir
9th July 2007, 17:43
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.

wysota
9th July 2007, 23:34
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.

iw2nhl
10th July 2007, 18:31
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 "&".

// Original text
QString text = "<text>";
// Encode it
QByteArray encoded = QUrl::toPercentEncoding("<text>", QByteArray(), "<>&");
// --> encoded == "%3Ctext%3E"
// Decode it
QString decoded = QUrl::fromPercentEncoding(encoded);
// --> decoded == "<text>"


2) use the new QXmlStreamWriter class which generates XML files, automatically encoding what is needed
See QXmlStreamWriter::writeCharacters() and QXmlStreamWriter::writeCDATA().

Vladimir
10th July 2007, 18:40
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

krsmichael
19th July 2007, 18:59
You do not want to do what the others have suggested. Use these routines for your DomElement, ect values.




QString EncodeXML
(
const QString& encodeMe
)
{
QString temp;

for (quint32 index(0); index < encodeMe.size(); index++)
{
QChar character(encodeMe.at(index));

switch (character.unicode())
{
case '&':
temp += "&amp;"; break;

case '\'':
temp += "&apos;"; break;

case '"':
temp += "&quot;"; break;

case '<':
temp += "&lt;"; break;

case '>':
temp += "&gt;"; break;

default:
temp += character;
break;
}
}

return temp;
}

QString DecodeXML
(
const QString& decodeMe
)
{
QString temp(decodeMe);

temp.replace("&amp;", "&");
temp.replace("&apos;", "'");
temp.replace("&quot;", "\"");
temp.replace("&lt;", "<");
temp.replace("&gt;", ">");

return temp;
}



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.