PHP XML DomDocument and pound symbol problem


I am using the PHP DomDocument class to create an XML file from data from a database.
Unfortunately, if the data in the dataabse contains a pound symbol (£), the whole field is ignored and not added to the XML.

Do you know what may be causing this?

My code to create the XML is;

$doc = new DomDocument('1.0');

// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);

// process one row at a time
while($row = mysql_fetch_assoc($result)) {
// add node for each row
  $occ = $doc->createElement('news');
  $occ = $root->appendChild($occ);
  // add a child node for each field
  foreach ($row as $fieldname => $fieldvalue) {
      $child = $doc->createElement($fieldname);
      $child = $occ->appendChild($child);
        $value = $doc->createTextNode($fieldvalue);
      $value = $child->appendChild($value);
  } // foreach
} // while
$xml_string = $doc->saveXML();
//echo $xml_string;
//$xml_string = html_entity_decode($xml_string);
$fp = @fopen('../xml/news.xml','w');
if(!$fp) {
    die('Error cannot create XML file');
Who is Participating?
SyntheticsConnect With a Mentor Commented:
I don't know why it does it, but you could get around it with replacement. When uploading to the database, replace '£' with something else that won't appear normally and then when retrieving it again, replace it back to '£'. It's a bit messy but works as an ad hoc solution if you're desperate to get it fixed while you hunt for a more permanent solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.