PHP XML DomDocument and pound symbol problem

Posted on 2007-10-05
Last Modified: 2008-03-14

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');
Question by:coolispaul
    1 Comment
    LVL 5

    Accepted 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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now