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 :/
<?php
class xml_parser {
private $file = 'plik.xml';
private $character_data_on = false;
private $tag_complete = true;
private $tablica = array(); private $name;
private $active = false;
private $records = false;
public function __construct()
{
$this->show();
}
private function startElement($parser, $name, $attrs)
{
$this->name = $name;
$this->tag_complete = false;
$this->character_data_on = false;
if($name == 'insert')
$this->records = true;
if($name == 'record')
$this->active = true;
}
private function endElement($parser, $name)
{
if (!$this->character_data_on)
{
$temp_fp = ftell($this->fp);
$end_element_byte_index = xml_get_current_byte_index($parser);
fseek($this->fp,$end_element_byte_index-2
);
$validator = fgets($this->fp, 3
); fseek($this->fp, $temp_fp);
$this->tag_complete = true;
}
$this->character_data_on = false;
{
$this->active = false;
$this->records = false;
}
}
private function characterData($parser, $data)
{
if ((!$this->character_data_on) && (!$this->tag_complete))
{
$this->tag_complete = true;
if($this->records && $this->active == true)
{
if($this->name != 'record')
{
if($this->name == 'fotos')
$this->tablica[0][$this->name]['foto'] = $data;
else
$this->tablica[0][$this->name] = $data;
}
}
}
$this->character_data_on = true;
}
private function show()
{
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler
($xml_parser, array($this, 'startElement'), array($this, 'endElement')); xml_set_character_data_handler
($xml_parser, array($this, 'characterData')); if (!($this->fp = fopen($this->file, "r"))) {
die('could not open XML input'); }
while ($file_content = fread($this->fp, 4096
)) {
if (!xml_parse
($xml_parser, $file_content, feof($this->fp))) {
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
}
}
$xml = new xml_parser();
?>
Plik xml:
<?xml version="1.0" encoding="Utf-8" standalone="yes"?>
<root>
<Name>TEST SAX</Name>
<UserName>achates</UserName>
<Full>True</Full>
<insert>
<record>
<obiekt>0</obiekt>
<transakcja>1</transakcja>
<id>140</id>
<ilefoto>0</ilefoto>
<fotos>
</fotos>
<cena>165000</cena>
<woj>5</woj>
<miasto>Konin</miasto>
<stan>do rem.</stan>
<uzbrojenie>CO</uzbrojenie>
<zmiany>2005.05.20</zmiany>
<Opis>Mieszkanie na parterze połączona z jednym z pokoi. </Opis>
</record>
<record>
<obiekt>0</obiekt>
<transakcja>1</transakcja>
<id>138</id>
<ilefoto>0</ilefoto>
<fotos>
<foto>foto1.jpg</foto>
<foto>foto2.jpg</foto>
</fotos>
<cena>245</cena>
<woj>5</woj>
<miasto>Warszawa</miasto>
<stan>po rem.</stan>
<uzbrojenie>CO</uzbrojenie>
<zmiany>2004.04.21</zmiany>
<Opis>mieszkanie na 8-tym piętrze, balkon, nowe okna. </Opis>
</record>
</insert>
</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
Ten post edytował Apo 24.05.2006, 17:52:18