Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][XML] problem z kodowaniem
Forum PHP.pl > Forum > PHP
_bolek_
Witam,

Mam taki maly problem z XML Pareser :/

otoż skożystałem z manuala dodalem co potrzebne zeby generowalo mi plik RDF+XML i postanowilem odczytac zawartosc pliku XML, nie stety mimo że plik po niekąd odzczytało poprawnie (bez błedów) to błędy sie pojawiły - złamało szyk danych dzielac dane na pol po napotkaniu na symbol kodowany w utf-8 :/

  1. <?php
  2.  
  3. class Parser {
  4.  
  5.  var $att, $name, $title, $about, $description, $date, $index=0, $xml_parser, $tab, $tagname;
  6.  
  7.  function parser($file) {
  8.   $this->xml_parser = xml_parser_create("UTF-8");
  9.   xml_set_object($this->xml_parser,$this);
  10.   xml_set_element_handler($this->xml_parser, "startElement", "endElement");
  11.   xml_set_character_data_handler($this->xml_parser, 'elementContent');
  12.   xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, TRUE);
  13.   xml_parser_set_option($this->xml_parser, XML_OPTION_SKIP_WHITE, TRUE);
  14.   xml_parser_set_option($this->xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
  15.  
  16.   if (!($fp = fopen($file, "r"))) {
  17.   die("could not open XML input");
  18.   }
  19.   while ($data = fread($fp, 4096)) {
  20.   $data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
  21.   if (!xml_parse($this->xml_parser, $data, feof($fp))) {
  22.   die(sprintf("XML error: %s at line %d",
  23.   xml_error_string(xml_get_error_code($this->xml_parser)),
  24.   xml_get_current_line_number($this->xml_parser)));
  25.   }
  26.   }
  27.   xml_parser_free($this->xml_parser);
  28.  }
  29.  
  30.  function startElement($parser, $name, $attrs) {
  31.   $this->att[$this->index++]=$name;
  32.   $this->tagname=$name;
  33.  }
  34.  
  35.  function elementContent($parser, $data) {
  36.   switch ($this->tagname){
  37.   case 'NAME' : $this->name[]=$data; break;
  38.   case 'TITLE' : $this->title[]=$data; break;
  39.   case 'ABOUT' : $this->about[]=$data; break;
  40.   case 'DESCRIPTION' : $this->description[]=$data; break;
  41.   case 'DATE' : $this->date[]=$data; break;
  42.   }
  43.  }
  44.  function endElement($parser, $name){
  45.   $this->tagname=="";
  46.  }
  47.  
  48.  function table(){
  49.   for ($i=0;$i<=($this->xml_parser);$i++){
  50.   $this->tab[$this->name[$i]]['name'] = $this->name[$i];
  51.   $this->tab[$this->name[$i]]['title'] = $this->title[$i];
  52.   $this->tab[$this->name[$i]]['about'] = $this->about[$i];
  53.   $this->tab[$this->name[$i]]['description'] = $this->description[$i];
  54.   $this->tab[$this->name[$i]]['date'] = $this->date[$i];
  55.   }
  56.   return $this->tab;
  57.  }
  58.  
  59.  function rdf_creator($site,$xhtml){
  60.   $tabs = $this->table();
  61.   $this->rdf .= '<?xml version="1.0"?>'."\n";
  62.   $this->rdf .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc= "http://purl.org/dc/elements/1.1/">'."\n";
  63.   $this->rdf .= '<rdf:Description rdf:about="'.$tabs[$site]['about'].'">'."\n";
  64.   $this->rdf .= '<dc:title>'.utf8_encode($tabs[$site]['title']).'</dc:title>'."\n";
  65.   $this->rdf .= '<dc:description>'.$tabs[$site]['description'].'</dc:description>'."\n";
  66.   $this->rdf .= '<dc:publisher>Refsnes Data as</dc:publisher>'."\n";
  67.   $this->rdf .= '<dc:date>'.$tabs[$site]['date'].'</dc:date>'."\n";
  68.   $this->rdf .= '<dc:type>Web Development</dc:type>'."\n";
  69.   $this->rdf .= '<dc:format>'.(($xhtml) ? "application/xhtml+xml" : "text/html").'</dc:format>'."\n";
  70.   $this->rdf .= '<dc:language>pl</dc:language>'."\n";
  71.   $this->rdf .= '</rdf:Description>'."\n";
  72.   $this->rdf .= '</rdf:RDF>'."\n";
  73.   return htmlspecialchars($this->rdf);
  74.  }
  75. }
  76. ?>


oto plik xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <rdfmap>
  3.  <site>
  4.        <name>main</name>
  5.        <title>połamalo dane1</title>
  6.        <about>http://www.example.pl/index.html</about>
  7.        <description>home</description>
  8.        <date>2008-07-13</date>
  9.  </site>
  10.  <site>
  11.        <name>kasy</name>
  12.        <title>połamalo dane2</title>
  13.        <about>http://www.example.pl/kasy.html</about>
  14.        <description>kasy fdfd fł</description>
  15.        <date>2008-07-13</date>
  16.  </site>
  17.  <site>
  18.        <name>zestawy</name>
  19.        <title>połamalo dane3</title>
  20.        <about>http://www.example.pl/zestawy.html</about>
  21.        <description>zestawy</description>
  22.        <date>2008-07-13</date>
  23.  </site>
  24.  <site>
  25.        <name>o nas</name>
  26.        <title>połamalo dane4</title>
  27.        <about>http://www.example.pl/onas.html</about>
  28.        <description>onas</description>
  29.        <date>2008-07-13</date>
  30.  </site>
  31. </rdfmap>


tablica wynikowa powinna być :

Kod
Array(
    [main] => Array(
        [name] => main
        [title] => połamalo dane1
        [about] => http://www.example.pl/index.html    
        [description] => home    
        [date] => 2008-07-13      
    )
    [kasy] => Array   ( 
        [name] => kasy
        [title] => połamalo dane2
        [about] => http://www.example.pl/kasy.html
        [description] => kasy
        [date] => 2008-07-13
    )
....
)


a jest:
Kod
Array(
    [main] => Array(
        [name] => main
        [title] => po <- brak dalszej czesci :/ przezucilo da do kolejnego elementu tablicy
        [about] => http://www.example.pl/index.html    
        [description] => home    
        [date] => 2008-07-13      
    )
    [kasy] => Array   ( 
        [name] => kasy
        [title] => łamalo dane1 < dalszy ciąg danych a powinno być co innego :/
        [about] => http://www.example.pl/kasy.html
        [description] => kasy
        [date] => 2008-07-13
    )
....
)


ma ktos pomysl jak to naprawić?
Zbychu666
Jako że miałem 5 minut do obiadu to poprawiłem smile.gif

Kod
<?php
header('Content-type: text/xml; charset="utf-8"');

class Parser {

  var $att, $name, $title, $about, $description, $date, $index=0, $xml_parser, $tab, $tagname;
  var $content;

  function parser($file)
  {
      $this->xml_parser = xml_parser_create("UTF-8");
      xml_set_object($this->xml_parser,$this);
      xml_set_element_handler($this->xml_parser, "startElement", "endElement");
      xml_set_character_data_handler($this->xml_parser, 'elementContent');
      xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, TRUE);
      xml_parser_set_option($this->xml_parser, XML_OPTION_SKIP_WHITE, TRUE);
      xml_parser_set_option($this->xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');

      if (!($fp = fopen($file, "r"))) {
          die("could not open XML input");
      }
      while ($data = fread($fp, 4096)) {
          $data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
          if (!xml_parse($this->xml_parser, $data, feof($fp))) {
              die(sprintf("XML error: %s at line %d",
              xml_error_string(xml_get_error_code($this->xml_parser)),
              xml_get_current_line_number($this->xml_parser)));
          }
      }
      xml_parser_free($this->xml_parser);
  }

  function startElement($parser, $name, $attrs) {
      $this->att[$this->index++]=$name;
      $this->tagname=$name;
  }

  function elementContent($parser, $data) {
     $this->content .= $data;
     /* przeniesione do endElement()
      switch ($this->tagname){
          case 'NAME'            : $this->name[]=$data; break;
          case 'TITLE'        : $this->title[]=$data; break;
          case 'ABOUT'        : $this->about[]=$data; break;
          case 'DESCRIPTION'    : $this->description[]=$data; break;
          case 'DATE'            : $this->date[]=$data; break;
      }
     */
  }
  function endElement($parser, $name){
      switch ($this->tagname){
          case 'NAME'            : $this->name[]=$this->content; break;
          case 'TITLE'        : $this->title[]=$this->content; break;
          case 'ABOUT'        : $this->about[]=$this->content; break;
          case 'DESCRIPTION'    : $this->description[]=$this->content; break;
          case 'DATE'            : $this->date[]=$this->content; break;
      }
      $this->content = '';
      $this->tagname = '';
  }

  function table(){
      for  ($i=0;$i < count($this->name);$i++){
          $this->tab[$this->name[$i]]['name'] = $this->name[$i];
          $this->tab[$this->name[$i]]['title'] = $this->title[$i];
          $this->tab[$this->name[$i]]['about'] = $this->about[$i];
          $this->tab[$this->name[$i]]['description'] = $this->description[$i];
          $this->tab[$this->name[$i]]['date'] = $this->date[$i];
      }
      return $this->tab;
  }

  function rdf_creator($site,$xhtml){
      $tabs = $this->table();
      $this->rdf .= ''."\n";
      $this->rdf .= ''."\n";
      $this->rdf .= ''."\n";
      $this->rdf .= ''.$tabs[$site]['title'].''."\n";
      $this->rdf .= ''.$tabs[$site]['description'].''."\n";
      $this->rdf .= 'Refsnes Data as'."\n";
      $this->rdf .= ''.$tabs[$site]['date'].''."\n";
      $this->rdf .= 'Web Development'."\n";
      $this->rdf .= ''.(($xhtml) ? "application/xhtml+xml" : "text/html").''."\n";
      $this->rdf .= 'pl'."\n";
      $this->rdf .= ''."\n";
      $this->rdf .= ''."\n";
      return $this->rdf;
  }
}

$file_name = 'plik.xml';
$Parser = new Parser($file_name);
echo $Parser->rdf_creator('main', true);

?>


W skrócie chodziło o to że funkcja xml_set_character_data_handler dla danych innych niż standardowe ASCII może być wywoływana wielokrotnie wewnątrz tego samego elementu.

+ jeszcze ze 2 drobne błędy poprawiłem.

P.S. Naciśnij tam jakiś "Pomógł", to będe miał dobre ratio. tongue.gif
_bolek_
ja akurat uzywam wywolania
  1. <?php
  2. header('Content-Type: application/xhtml+xml; charset=utf-8');
  3. ?>


jakos bardziej preferuje gdy opera za mnie kontroluje czy skladnia wynikowa pliku jest poprawna biggrin.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.