Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2013, 11:26 AM   PM User | #1
mr curious
New Coder

 
Join Date: Jan 2011
Posts: 55
Thanks: 7
Thanked 0 Times in 0 Posts
mr curious is an unknown quantity at this point
Can't decode special characters from xml

Hi all,

I've got this script which converts data from a mysql database to an xml object using the DOM and printing it out. Everything works fine, however some of the text contains ampersands (&) which gets printed as '&amp'

here's the script:
PHP Code:
<? 
function getdata($type

    
$mysqHost "xxx"
    
$mysqlUsr "xxx"
    
$mysqlPass "xxx"
     
     
    
$conn mysql_connect($mysqHost,$mysqlUsr,$mysqlPass); 
     
    
mysql_select_db("xxx") ; 

    
$sql = ("SELECT * FROM `$type`;"); 
    
$result =  mysql_query($sql); 
    
$doc = new DomDocument('1.0'); 
    
$type $str str_replace(' ','',$type); 

    
$root $doc->createElement($type.'List'); 
        
$root $doc->appendChild($root);   
    while(
$row mysql_fetch_assoc($result)) 
    { 
         
       
$company$doc->createElement($type); 
       
$company $root->appendChild($company);     
       foreach(
$row as $fieldname => $fieldvalue
       { 
            if(
$fieldname !='id'
            { 
            
$child $doc->createElement($fieldname); 
            
$child $company->appendChild($child); 
            
//add data to the new element 
            
$value $doc->createTextNode($fieldvalue); 
            
$value $child->appendChild($value); 
            } 
             
        } 
    } 
    
$str $doc->saveXML(); 

    print 
$str



$type $_REQUEST['type']; 
        
getdata($type);     

if(
$conn){ mysql_close($conn); } 
?>
I've tried to decode $fieldValue using html_entity_decode, but it doesn't work. I really don't know how to fix this. Any help would be greatly appreciated.

Thanks

Last edited by mr curious; 02-21-2013 at 11:27 AM.. Reason: unneeded code
mr curious is offline   Reply With Quote
Old 02-21-2013, 01:05 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Use createCDATASection instead of createTextNode. That should fix that issue.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mr curious (02-21-2013)
Old 02-21-2013, 01:15 PM   PM User | #3
mr curious
New Coder

 
Join Date: Jan 2011
Posts: 55
Thanks: 7
Thanked 0 Times in 0 Posts
mr curious is an unknown quantity at this point
Thanks It worked! I can't believe I struggled for such a long time and there was such a simple answer.

Thanks again Fou-Lu
mr curious is offline   Reply With Quote
Old 02-21-2013, 01:18 PM   PM User | #4
mr curious
New Coder

 
Join Date: Jan 2011
Posts: 55
Thanks: 7
Thanked 0 Times in 0 Posts
mr curious is an unknown quantity at this point
Just out of interest, howcome createCDATASection works as opposed to createTextNode?
mr curious is offline   Reply With Quote
Old 02-21-2013, 01:29 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
CDATA sections are non-parseable character data while text nodes are text blocks inserted into the XML which will parse all non-CDATA blocks (ie: PCDATA sections). & is registered as an XML entity, so it automatically coverts it. Things like < and > will also automatically convert to their &lt; &gt; entities when writing using the dom.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
dom, url_decode, xml

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:29 PM.


Advertisement
Log in to turn off these ads.