PDA

View Full Version : QXmlStreamWriter UTF-16 Problem



27Loco
6th July 2010, 15:24
Heho,

I have a problem with the QXmlStreamWriter class. When using UTF-16 encoding garbage characters will appear in my XML document.



QXmlStreamWriter xml;
xml.setDevice(&file);
// here is the problem
xml.setCodec(QTextCodec::codecForName("utf16"));
xml.setAutoFormatting(true);
xml.setAutoFormattingIndent(2);

xml.writeStartDocument();

xml.writeDTD("<!DOCTYPE backup>");
xml.writeStartElement("Backup");
xml.writeAttribute("version", "1.0");

xml.writeStartElement("Task");
xml.writeAttribute("name", "MyName");
xml.writeTextElement("Description", "My short description");
xml.writeEndElement(); // Task

xml.writeEndElement();
xml.writeEndDocument();


Output:


<?xml version="1.0" encoding="UTF-16"?>਍㰀℀䐀伀䌀吀 ¤€å€€ä”€â€‚æˆ€æ„€æŒ æ¬€ç”€ç€€ã¸€à´€
<Backup version="1.0">à¨â€‚â€‚ã°€å€æ„€çŒ æ¬€â€‚æ¸€æ„€æ´€æ”€ã´ €âˆ€ä´€ç¤€â€‚æ˜€æ¤€çˆ €çŒ€ç€â€‚æˆ€æ„€æŒ€æ €ç”€ç€€âˆ€ã¸€à´€
<Description>My short description</Description>à¨â€‚â€‚â€‚â€‚ã°€ä” €æ¸€æ„€æˆ€æ°€æ”€æ€ã €ç€çˆ€ç”€æ”€ã°€â¼€ä ”€æ¸€æ„€æˆ€æ°€æ”€æ€ 㸀ഀ
etc....


Do you have any idea? Thank you very much for your help.

Best regards,


Loco

dbzhang800
7th July 2010, 03:32
Qt 4.6.0 + VS2008 + windows Xp sp3


<?xml version="1.0" encoding="UTF-16"?>
<!DOCTYPE backup>
<Backup version="1.0">
<Task name="MyName">
<Description>My short description</Description>
</Task>
</Backup>



fffe 3c00 3f00 7800 6d00 6c00 2000 7600
6500 7200 7300 6900 6f00 6e00 3d00 2200
3100 2e00 3000 2200 2000 6500 6e00 6300
6f00 6400 6900 6e00 6700 3d00 2200 5500
5400 4600 2d00 3100 3600 2200 3f00 3e00
0a00 3c00 2100 4400 4f00 4300 5400 5900
5000 4500 2000 6200 6100 6300 6b00 7500
7000 3e00 0a00 3c00 4200 6100 6300 6b00
7500 7000 2000 7600 6500 7200 7300 6900
6f00 6e00 3d00 2200 3100 2e00 3000 2200
3e00 0a00 2000 2000 3c00 5400 6100 7300
6b00 2000 6e00 6100 6d00 6500 3d00 2200
4d00 7900 4e00 6100 6d00 6500 2200 3e00
0a00 2000 2000 2000 2000 3c00 4400 6500
7300 6300 7200 6900 7000 7400 6900 6f00
6e00 3e00 4d00 7900 2000 7300 6800 6f00
7200 7400 2000 6400 6500 7300 6300 7200
6900 7000 7400 6900 6f00 6e00 3c00 2f00
4400 6500 7300 6300 7200 6900 7000 7400
6900 6f00 6e00 3e00 0a00 2000 2000 3c00
2f00 5400 6100 7300 6b00 3e00 0a00 3c00
2f00 4200 6100 6300 6b00 7500 7000 3e00
0a00 0a

dbzhang800
7th July 2010, 03:41
OK, I know

you using windows and your file opened with QIODevice::Text, So

"0a00 ..." ==> "0d0a 00..."


fffe 3c00 3f00 7800 6d00 6c00 2000 7600
6500 7200 7300 6900 6f00 6e00 3d00 2200
3100 2e00 3000 2200 2000 6500 6e00 6300
6f00 6400 6900 6e00 6700 3d00 2200 5500
5400 4600 2d00 3100 3600 2200 3f00 3e00
0d0a 003c 0021 0044 004f 0043 0054 0059
0050 0045 0020 0062 0061 0063 006b 0075
0070 003e 000d 0a00 3c00 4200 6100 6300
6b00 7500 7000 2000 7600 6500 7200 7300
6900 6f00 6e00 3d00 2200 3100 2e00 3000
2200 3e00 0d0a 0020 0020 003c 0054 0061
0073 006b 0020 006e 0061 006d 0065 003d
0022 004d 0079 004e 0061 006d 0065 0022
003e 000d 0a00 2000 2000 2000 2000 3c00
4400 6500 7300 6300 7200 6900 7000 7400
6900 6f00 6e00 3e00 4d00 7900 2000 7300
6800 6f00 7200 7400 2000 6400 6500 7300
6300 7200 6900 7000 7400 6900 6f00 6e00
3c00 2f00 4400 6500 7300 6300 7200 6900
7000 7400 6900 6f00 6e00 3e00 0d0a 0020
0020 003c 002f 0054 0061 0073 006b 003e
000d 0a00 3c00 2f00 4200 6100 6300 6b00
7500 7000 3e00 0d0a 000a

27Loco
7th July 2010, 14:04
You are right. I used QIODevice::Text. This was the problem. Thank you very much for the solution :).