• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1927
  • Last Modified:

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');
1 Solution
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now