Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: odczytanie xml -- SAX
Forum PHP.pl > Forum > PHP > Object-oriented programming
Apo
Witam
Znalazłem ostatnio w manualu przykład wykorzystania SAX i przerobiłem go do włanych potrzeb. Ale mam kilka drognych błędów :/

  1. <?php
  2.  
  3. class xml_parser {
  4.  
  5. private $file = 'plik.xml';
  6. private $character_data_on = false;
  7. private $tag_complete = true;
  8. private $tablica = array();
  9. private $name;
  10. private $active = false;
  11. private $records = false;
  12.  
  13. public function __construct()
  14. {
  15. $this->show();
  16. echo '<pre>'.print_r($this->tablica, 1).'</pre>';
  17. }
  18.  
  19. private function startElement($parser, $name, $attrs) 
  20. {
  21.  $this->name = $name;
  22.  $this->tag_complete = false;
  23.  $this->character_data_on = false;
  24.  
  25. if($name == 'insert')
  26. $this->records = true;
  27.  
  28. if($name == 'record')
  29. $this->active = true;
  30. }
  31.  
  32. private function endElement($parser, $name) 
  33. {
  34.  
  35.  if (!$this->character_data_on)
  36.  {
  37.  $temp_fp = ftell($this->fp);
  38.  
  39.  $end_element_byte_index = xml_get_current_byte_index($parser);
  40.  fseek($this->fp,$end_element_byte_index-2);
  41.  
  42.  $validator = fgets($this->fp, 3);
  43.  fseek($this->fp, $temp_fp);
  44.  
  45.  $this->tag_complete = true;
  46.  }
  47.  
  48.  $this->character_data_on = false;
  49.  
  50. if(preg_match('#record#', $name))
  51. {
  52. $this->active = false;
  53. $this->records = false;
  54. }
  55. }
  56.  
  57. private function characterData($parser, $data) 
  58. {
  59.  
  60.  
  61.  if ((!$this->character_data_on) && (!$this->tag_complete))
  62.  {
  63.  $this->tag_complete = true;
  64.  
  65.  if($this->records && $this->active == true)
  66.  {
  67. if($this->name != 'record')
  68. {
  69. if($this->name == 'fotos')
  70. $this->tablica[0][$this->name]['foto'] = $data;
  71. else
  72. $this->tablica[0][$this->name] = $data;
  73. }
  74.  }
  75.  }
  76.  
  77.  $this->character_data_on = true;
  78. }
  79.  
  80. private function show()
  81. {
  82.  
  83. $xml_parser = xml_parser_create();
  84. xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
  85. xml_set_element_handler($xml_parser, array($this, 'startElement'), array($this, 'endElement'));
  86. xml_set_character_data_handler($xml_parser, array($this, 'characterData'));
  87. if (!($this->fp = fopen($this->file, "r")))
  88. {
  89. die('could not open XML input');
  90. }
  91.  
  92. while ($file_content = fread($this->fp, 4096))
  93. {
  94. if (!xml_parse($xml_parser, $file_content, feof($this->fp)))
  95. {
  96.  die(sprintf('XML error: %s at line %d',
  97.  xml_error_string(xml_get_error_code($xml_parser)),
  98.  xml_get_current_line_number($xml_parser)));
  99. }
  100. }
  101.  
  102. xml_parser_free($xml_parser);
  103. }
  104.  
  105. }
  106.  
  107. $xml = new xml_parser();
  108. ?>


Plik xml:
  1. <?xml version="1.0" encoding="Utf-8" standalone="yes"?>
  2. <root>
  3.        <Name>TEST SAX</Name>
  4.        <UserName>achates</UserName>
  5.        <Full>True</Full>
  6.    <insert>
  7.    <record>
  8.       <obiekt>0</obiekt>
  9.       <transakcja>1</transakcja>
  10.       <id>140</id>
  11.       <ilefoto>0</ilefoto>
  12.       <fotos>
  13.       </fotos>
  14.       <cena>165000</cena>
  15.       <woj>5</woj>
  16.       <miasto>Konin</miasto>
  17.       <stan>do rem.</stan>
  18.       <uzbrojenie>CO</uzbrojenie>
  19.       <zmiany>2005.05.20</zmiany>
  20.       <Opis>Mieszkanie na parterze poĹ‚Äczona z jednym z pokoi.    </Opis>
  21.    </record>
  22.    <record>
  23.       <obiekt>0</obiekt>
  24.       <transakcja>1</transakcja>
  25.       <id>138</id>
  26.       <ilefoto>0</ilefoto>
  27.       <fotos>
  28.     <foto>foto1.jpg</foto>
  29.     <foto>foto2.jpg</foto>
  30.       </fotos>
  31.       <cena>245</cena>
  32.       <woj>5</woj>
  33.       <miasto>Warszawa</miasto>
  34.       <stan>po rem.</stan>
  35.       <uzbrojenie>CO</uzbrojenie>
  36.       <zmiany>2004.04.21</zmiany>
  37.       <Opis>mieszkanie na 8-tym piÄtrze, balkon, nowe okna.    </Opis>
  38.    </record>
  39.    </insert>
  40. </root>


Skrypt odczytuje mi wszystko co jest między tagami <record> (tylko 1 tag odczyta) i zapisuje do tablicy i wyświetla. Ale problem jest gdy jest tag <fotos> to nie potrafie zapisac do tablicy tych pod elementów <foto> i z powrotem zamknąć tagu <fotos>. Drugi problem dotyczy pola <Opis> bo nie wiem czemu obcina mu wartość (nie wyświetla całej).

Z góry WIELKI DZIĘKI za pomoc ...

Pozdrawiam

btw bardzo ładnie wam tabelki wyszły tongue.gif
Apo
Już rozwiązałem problem pod elementów '<foto>' i ucinania opisu. Opis ucina ponieważ gdy parser natrafi na znak specjalny np 'ó', 'ś' to takjakby to pomija i przechodzi dalej. No i mam problem bo kodowanie jest chyba dobre np 'ó' = 'Ăł' a i tak ucina mi tam tekst :'(
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.