SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SmartQuotes in XML feed are being converted to quertion marks (?)

    I am using this class to parse my XML:
    Code:
    <?php
    /* 
    
    XML Parser Class
    by Eric Rosebrock
    http://www.phpfreaks.com
    
    Class originated from: kris@h3x.com AT: http://www.devdump.com/phpxml.php
    
    Usage:
    
    <?php
    include 'clsParseXML.php';
    
    $xmlparse = &new ParseXML;
    $xml = $xmlparse->GetXMLTree('/path/to/xmlfile.xml');
    
    echo "<pre>";
    print_r($xml);
    echo "</pre>";
    ?>
    
    The path to the XML file may be a local file or a URL.
    Returns the elements of the XML file into an array with
    it's subelements as keys and subarrays.
    
    */
    
    class ParseXML{
          function GetChildren($vals, &$i) { 
             $children = array(); // Contains node data
             if (isset($vals[$i]['value'])){
                $children['VALUE'] = $vals[$i]['value'];
             } 
             
             while (++$i < count($vals)){ 
                switch ($vals[$i]['type']){
                   
                case 'cdata': 
                   if (isset($children['VALUE'])){
                      $children['VALUE'] .= $vals[$i]['value'];
                   } else {
                      $children['VALUE'] = $vals[$i]['value'];
                   } 
                break;
                
                case 'complete':
                   if (isset($vals[$i]['attributes'])) {
                      $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                      $index = count($children[$vals[$i]['tag']])-1;
             
                      if (isset($vals[$i]['value'])){ 
                         $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value']; 
                      } else {
                         $children[$vals[$i]['tag']][$index]['VALUE'] = '';
                      }
                   } else {
                      if (isset($vals[$i]['value'])){
                         $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value']; 
                      } else {
                         $children[$vals[$i]['tag']][]['VALUE'] = '';
                      } 
                   }
                break;
                
                case 'open': 
                   if (isset($vals[$i]['attributes'])) {
                      $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                      $index = count($children[$vals[$i]['tag']])-1;
                      $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
                   } else {
                      $children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
                   }
                break; 
             
                case 'close': 
                   return $children; 
             } 
          }
       }
         
          function GetXMLTree($xmlloc){ 
             if (file_exists($xmlloc)){
                $data = implode('', file($xmlloc)); 
             } else {
                $fp = @fopen($xmlloc,'r');
                while(!@feof($fp)){
                   $data = $data . @fread($fp, 1024);
                }      
                @fclose($fp);
             }
          
             $parser = xml_parser_create('ISO-8859-1');
             xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
             xml_parse_into_struct($parser, $data, $vals, $index); 
             xml_parser_free($parser); 
          
             $tree = array(); 
             $i = 0; 
          
             if (isset($vals[$i]['attributes'])) {
                $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; 
                $index = count($tree[$vals[$i]['tag']])-1;
                $tree[$vals[$i]['tag']][$index] =  array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
             } else {
                $tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i); 
             }
          return $tree; 
          }
    }
    The format of the XML file is like this:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    After parsing the file I try and echo the result
    and all of the quote marks are displaying as ? marks...

    I have tried converting the field using utf8_encode and htmlentities, but to no avail.

    Why is this happening? Are these smart quotes outside the UTF-8 character set?

    What can I do about it?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Load the xml file into FF and see how it is actually encoded, vs what the opening declaration states.


Bookmarks

Posting Permissions

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