PHP reads the records in the database to generate XML. how can this problem be solved when the records contain Chinese characters?

Source: Internet
Author: User
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: & lt ;? & Nbsp; require ('Conn. php'); $ owen1 = 'Student Work'; header (what should I do if the record contains Chinese characters when 'co php reads the records in the database to generate XML?
I want to use the PHP DOM function to generate an XML document. if the document contains 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 ();

}
Run the command, and you will be prompted to encounter unformatted characters in the latest news. how can I change it? share it:
------ Solution --------------------
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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.