Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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 12:27 PM. Reason: unneeded code

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Use createCDATASection instead of createTextNode. That should fix that issue.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    mr curious (02-21-2013)

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Just out of interest, howcome createCDATASection works as opposed to createTextNode?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •