![]() |
![]() ![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 9 Pomógł: 0 Dołączył: 13.07.2008 Ostrzeżenie: (0%) ![]() ![]() |
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 :/
oto plik xml
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ć? Ten post edytował _bolek_ 13.07.2008, 11:43:27 |
|
|
![]() ![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 14 Pomógł: 5 Dołączył: 13.07.2008 Ostrzeżenie: (0%) ![]() ![]() |
Jako że miałem 5 minut do obiadu to poprawiłem
![]() 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. ![]() Ten post edytował Zbychu666 13.07.2008, 17:34:22 |
|
|
![]()
Post
#3
|
|
Grupa: Zarejestrowani Postów: 9 Pomógł: 0 Dołączył: 13.07.2008 Ostrzeżenie: (0%) ![]() ![]() |
|
|
|
![]() ![]() |
![]() |
Wersja Lo-Fi | Aktualny czas: 13.06.2025 - 06:23 |