PDA

View Full Version : Problem in XML parsing ";" (semicolon) char



the_bis
1st June 2007, 10:05
Hi,
I have a problem with XML library (Qt DOM class).
My XML result file is like:


<!DOCTYPE myConfigFile>
<myFileSettings>
<option key="key_name" value="key_value" />
</myFileSettings>

How can I handle a value like "key_value1;key_value2;key_value3" ?
Now if I use the semicolon char ";" I obtain a wrong XML file...

Thanks for the help,
the_bis

wysota
1st June 2007, 10:37
How about just changing the structure?
<option key="key_name">key_value1;key_value2;...</option>

Anyway I don't see a problem why the original idea shouldn't work... What is the exact problem you're facing?

the_bis
1st June 2007, 10:56
How about just changing the structure?
<option key="key_name">key_value1;key_value2;...</option>


I have to mantain this structure :(


Anyway I don't see a problem why the original idea shouldn't work... What is the exact problem you're facing?

I have to read a user input with a QLineEdit (this is a remote command to launch via SSH). If I receive a string like "key1;key2;" this is the generated XML file:


<!DOCTYPE myConfigFile>
<myFileSettings>
<option key="connection name" value="MyServer" />
<option key="remote command" value="key1


The file doesn't end correctly: it ends after the first ";" and I have a wrong formatted XML file :(

Any help?
Thanks,
the_bis

wysota
1st June 2007, 11:07
I have to read a user input with a QLineEdit (this is a remote command to launch via SSH).

In that case I even stronger suggest to use my suggestion and even wrap everything in a CDATA section. If you don't do that, you're asking for trouble. The user can enter virtually anything as a command (including characters ">", "<" which can't be part of attribute values). So either you encode the data (either in some completely mangled form or using SGML entities) in attribute values or you have to use CDATA.

the_bis
1st June 2007, 11:19
In that case I even stronger suggest to use my suggestion and even wrap everything in a CDATA section. If you don't do that, you're asking for trouble. The user can enter virtually anything as a command (including characters ">", "<" which can't be part of attribute values). So either you encode the data (either in some completely mangled form or using SGML entities) in attribute values or you have to use CDATA.

I'm trying to solve the problem encoding special chars that can have troubles:

; -> [MY_SEMICOLON]
" -> [MY_QUOTE]
& -> [MY_AMPERSEND]
> -> [MY_MORETHAN]
< -> [MY_LESSTHAN]


I hope this is enough :crying:
the_bis

wysota
1st June 2007, 11:26
It'd be really easier if you changed the structure.
And if you want to encode, I suggest this:
& => &amp;
" => &quot;
> => &gt;
< => &lt;
' => &apos;

Also encode every character with ascii value greater than 127 as &xxx; where xxx is the hex value of the ascii value. If you don't, you're likely to run into problems with encoding.

The semicolon should be fine by itself... I don't know why it doesn't work for you.

wysota
1st June 2007, 11:31
Could you try running this application?

#include <qdom.h>
#include <qstring.h>


int main(){
QDomDocument doc;
QDomElement root = doc.createElement("root");
doc.appendChild(root);
QDomElement elem = doc.createElement("test");
elem.setAttribute("arg", "value1;value2;value3");
root.appendChild(elem);
qDebug("%s", doc.toString().ascii());

return 0;
}

It works for me without problems, so if semicolons are causing you trouble, the problem is within your code and not Qt.

the_bis
1st June 2007, 12:21
Could you try running this application?


Yes, it works also for me! :o

Sorry sorry sorry: the error was in another place of my source code...

Thank you again for your help and to let me to understand that also my XML code was right,
the_bis