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.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();
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();
To copy to clipboard, switch view to plain text mode
Output:
<?xml version="1.0" encoding="UTF-16"?>à¨ã°€â„€ä€ä¼€äŒ€å€å¤€å€€ä”€â€‚戀愀挀欀甀瀀㸀ഀ
<Backup version="1.0">à¨â€‚ 㰀å€æ„€çŒ€æ¬€â€‚渀愀洀攀㴀∀䴀礀 昀椀爀猀ç€â€‚戀愀挀欀甀瀀∀㸀ഀ
<Description>My short description</Description>à¨â€‚   㰀䔀渀愀戀氀攀æ€ã¸€ç€çˆ€ç”€æ”€ã°€â¼€ä”€æ¸€æ„€æˆ€æ°€æ”€æ€ã¸€à´€
etc....
<?xml version="1.0" encoding="UTF-16"?>à¨ã°€â„€ä€ä¼€äŒ€å€å¤€å€€ä”€â€‚戀愀挀欀甀瀀㸀ഀ
<Backup version="1.0">à¨â€‚ 㰀å€æ„€çŒ€æ¬€â€‚渀愀洀攀㴀∀䴀礀 昀椀爀猀ç€â€‚戀愀挀欀甀瀀∀㸀ഀ
<Description>My short description</Description>à¨â€‚   㰀䔀渀愀戀氀攀æ€ã¸€ç€çˆ€ç”€æ”€ã°€â¼€ä”€æ¸€æ„€æˆ€æ°€æ”€æ€ã¸€à´€
etc....
To copy to clipboard, switch view to plain text mode
Do you have any idea? Thank you very much for your help.
Best regards,
Loco
Bookmarks