Xml operations in symbian
This is the code for writing, exporting, and importing xml files. A problem is that when the exported data contains Chinese characters, the Chinese characters written into the xml file are garbled, so that when the xml file is imported, CParser reported an error of-996. Later, he tried to convert the Chinese characters in xml into GBK and convert it into gbk using the CCnvCharacterSetConverter class method. In this way, when the exported xml is opened in notepad, the Chinese characters can be displayed normally, however, CParser still cannot be parsed during parsing, which is quite depressing. If you want to use another xml parsing method, but c ++ is not very familiar with it, use this CParser. I forgot to say it, when writing xml, I write a class by myself and write data through RRileWriteStream. Because it cannot be parsed, I keep asking my friends in the group and searching online. The results are almost all about using UTF-8 encoding. However, if the file is written to utf8 encoding, I will not elaborate on it, another example is to use Notepad for conversion, but this is not a solution. After repeated tests, we found that when writing xml, if the xml encoding = "UTF-8 ", open the binary with ue32 and find that the write is 16 bits. If this encoding is removed, it is 8 bits. I don't know why.
This problem has not been solved yet. Later, I accidentally found the xml Generation Code provided by symbian. It was written by mark and written through CSenDomFragment. I tried to write it. It's okay. The write was successful, however, Chinese characters are garbled. Try to import the CParser. The CParser does not report an error. Haha, the problem is solved. The problem is changed to Chinese characters, and it is garbled. One problem is solved, and the other is solved. I have been checking how to solve this problem. I will check the encoding. symbian uses unicode and I will debug the breakpoint, check whether it is unicode encoding at the time of writing. Well, I found that unicode encoding is used during writing. I used TDesC8 to write data to a file, and the Chinese character converted TDesC16 to TDesC8 before writing data to the file, this is where the problem cannot be converted like this. Later, I changed unicode to utf8, wrote the file, exported the xml, checked the file, and finally saw the Chinese characters. Hurry up and check whether the data can be imported, well, there is no problem with the import, but it is still garbled. This is a good solution, because now it is utf8 encoding, converted to unicode, haha, Chinese characters are also displayed, and finally solved, later, I tried to generate the xml class written by myself. It seems that the final problem is the encoding during file writing.
How is utf8 and unicode converted? Use the static method in the CnvUtfConverter class to complete the conversion. As a result, the xml problem that has plagued the last two weeks has finally ended !!!
The following code is provided:
Void CXmlNode: SetValue (const TDesC16 & aValue) {if (! This-> iNodeValue) {iNodeValue = HBufC8: NewL (aValue. length () * 3);} else {iNodeValue-> Des (). zero (); iNodeValue = iNodeValue-> ReAlloc (aValue. length () * 3);} TPtr8 p = iNodeValue-> Des (); if (CnvUtfConverter: ConvertFromUnicodeToUtf8 (p, aValue) = character: EErrorIllFormedInput) {User :: leave (CCnvCharacterSetConverter: EErrorIllFormedInput );}
}
Anping 2009 @ original
Qi_jianzhou@126.com