When PHP reads the records in the database to generate XML, the records contain Chinese characters. what should I do if I want to use the DOM function of PHP to generate XML documents? if the documents contain Chinese characters, an error occurs, that is, it is useless to use CDATA to contain Chinese characters, the code is as follows:
$ Owen1 = 'Student Work ';
Header ('content-type: text/XML ');
$ Result = $ conn-> query ("select * from news where BigClassName = '$ owen1 'limit 4 ");
If ($ result)
{
$ Dom = new DOMDocument ('1. 0', 'utf-8 ');
$ Dom-> formatOutput = true;
$ Rcs = $ dom-> createElement ("rss ");
While ($ row = $ result-> fetch_assoc ()){
// Print_r ($ arr );
$ Rc = $ dom-> createElement ("channel ");
$ Mydate = $ dom-> createElement ("title ");
$ Myname = $ dom-> createElement ("link ");
$ Mydescription = $ dom-> createElement ("description ");
$ Myhp = $ dom-> createElement ("item ");
$ Date = $ row ['title'];
$ Datetext = $ dom-> createTextNode ('最新动态');
$ Mydate-> appendChild ($ datetext );
$ Nametext = $ dom-> createTextNode ($ name );
$ Myname-> appendChild ($ nametext );
$ Hptext = $ dom-> createTextNode ($ hp );
$ Myhp-> appendChild ($ hptext );
$ Descriptiontext = $ dom-> createTextNode ($ description );
$ Mydescription-> appendChild ($ descriptiontext );
$ Rcs-> appendChild ($ rc );
$ Rc-> appendChild ($ mydate );
$ Rc-> appendChild ($ myname );
$ Rc-> appendChild ($ myhp );
$ Rc-> appendChild ($ mydescription );
}
$ Dom-> appendChild ($ rcs );
Echo $ dom-> saveXML ();
}
Execute the command, and you will be prompted to encounter unformatted characters in the latest news. how can this problem be changed?
Reply to discussion (solution)
In addition, my php file is encoded as gb2312.
Data to be written into XML using UTF-8 encoding
Two solutions:
1. The data read from the database in your code is transcoded using the iconv function.
$ Date = iconv ('gbk', utf-8 ', $ row ['title']);
$ Datetext = $ dom-> createTextNode (iconv ('gbk', utf-8 ','最新动态'));
Other similar
2. read UTF-8 encoded data directly from the database
$ Conn-> query ("set nemes utf8 ");
$ Owen1 = iconv ('gbk', utf-8 ', 'Student Work ');
Others remain unchanged