Helping ordinary people create extraordinary websites!

Using PHP 4s DOM XML Functions to Create XML Files from SQL Data

By Tony Marston
2005-04-04

Using Multi-Byte Characters
I had a slight problem recently when the data I output to my XML file contained characters with accents (as in à, è, í, ö and û). These were coming out all garbled until I discovered the reason why. It turns out that the internal encoding for libxml when storing the document is UTF-8, so you need to convert non UTF-8 encoded strings into UTF-8 when setting content. This requires the following changes to the code samples:

(1) Convert from default character set (refer to default_charset in file php.ini) to UTF-8 by inserting a single line as follows:

  foreach ($row as $fieldname => $fieldvalue) {

$child = $doc->create_element($fieldname);
$child = $inner->append_child($child);
$value = mb_convert_encoding($value,'UTF-8','ISO-8859-1'); <<-- new line
$value = $doc->create_text_node($fieldvalue);
$value = $child->append_child($value);
} // foreach
(2) To output the document correctly you must convert back to the default character set by amending the following line:

$xml_string = $doc->dump_mem(true, 'ISO-8859-1');
NOTE: In order for this to work you must enable the Multi-Byte String functions in PHP.



Tutorial pages:
 1 Votes

You might also want to check these out:


Leave a Comment on "Using PHP 4s DOM XML Functions to Create XML Files from SQL Data"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS