Portál AbcLinuxu, 30. dubna 2025 21:23
Již delší dobu jsem nic nenapsal o mých zkušenostech s PHP. Jak je z nadpisu partné zkoušel jsem parsování XML v PHP, ale nějak mi to nevychází.
Na parsování jsem si vytvořil jednoduchý soubor XML:
<?xml version="1.0" encoding="UTF-8"?> <apcpinstall> <type>component</type> <name>test</name> <author>Filip Bartmann</author> <e-mail>xxx@yyy.info</e-mail> <license>GNU-GPL</license> <sql type="install">CREATE TABLE #__test (test_id int(5) NOT NULL auto_increment, test_name mediumtext NOT NULL, PRIMARY KEY(test_id))</sql> <sql type="uninstall">DROP TABLE #_test</sql> <files> <file id="main">test.php</file> <file id="menu">menu.php</file> <file id="lang">cs_CZ.utf8</file> <file id="lang">en_GB.utf8</file> </files> </apcpinstall>
Kód pársuji pomocí této třídy:
class xmlparse { public $langs=Array(); function begin($parser, $name, $attr) { $this->xml = $name; $this->attr = $attr; $this->pd = 0; } function stop($parser, $name) { if ($name == "apcpinstall") { switch ($this->type) { case "component": $sql = "INSERT INTO #__components SET comp_name=\"".$this->name."\", comp_url=\"com=".$this->name."\", comp_include=\"".$this->name."/".$this->main."\", comp_menu=\"".$this->name."/".$this->menu."\",core=0"; //dbcon($sql); break; default: break; } } } function data($parser, $data) { if ($this->pd == 0) { echo $this->xml." ".$data."<br>"; switch ($this->xml) { case "type" : if ($data == "component") { $this->type = "component"; } break; case "name" : $this->name = $data; case "sql" : if (isset ($this->attr["type"])) { if ($this->attr["type"] == "install") { //dbcon($data); } } break; case "file" : switch ($this->attr["id"]) { case "main" : $this->main = $data; break; case "menu" : $this->menu = $data; case "lang" : array_push($this->langs,$data); default : break; } break; default : break; } $this->pd++; } } function parse($cname) { global $basePath; $this->parser = xml_parser_create(); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, "begin", "stop"); xml_set_character_data_handler($this->parser, "data"); if (file_exists($basePath.'/tmp/install/'.basename($_FILES["comp"]["tmp_name"]).'/'.$cname.'/install.xml')) { $xmlfile = fopen($basePath.'/tmp/install/'.basename($_FILES["comp"]["tmp_name"]).'/'.$cname.'/install.xml', "r"); if (!$xmlfile) { echo "Soubor install.xml se nepodařilo otevřít<br/>"; return 0; } else { while ($xmldata = fread($xmlfile, filesize($basePath.'/tmp/install/'.basename($_FILES["comp"]["tmp_name"]).'/'.$cname.'/install.xml'))) { xml_parse($this->parser, $xmldata); } xml_parser_free($this->parser); $files=Array($this->main, $this->menu, $this->langs); return $files; } } else { echo "Soubor install.xml neexistuje<br>"; return 0; } } }
Když si dám hodnoty vytisknout pomocí řádku zvýrazněného kurzívou zobrazí se mi toto:
apcpinstall type component type name test name author Filip Bartmann author e-mail xxx@yyy.info e-mail license GNU-GPL license sql CREATE TABLE #__test (test_id int(5) NOT NULL auto_increment, test_name mediumtext NOT NULL, PRIMARY KEY(test_id)) sql sql DROP TABLE #_test sql files file test.php file file menu.php file file cs_CZ.utf8 file file en_GB.utf8 file file
A tady je ten problém. Nemůžu zjistit proč se mi zdvojují ty otevírací tagy, tedy proč tam mám jednou type component a podruhé samotné type? Provizorně jsem to vyřešil pomocí řádků zvýrazněných tučně, ale vím, že je to špatné řešení a také bych se to docela rád naučil správně.
Druhý problém je, proč se mi v poli files v podpoli langs objevuju menu.php, když ho tam vůbec nedávám:
Array ( [0] => test.php [1] => menu.php [2] => Array ( [0] => menu.php [1] => cs_CZ.utf8 [2] => en_GB.utf8 ) )
Nemůžu přijít na to, kde mám chybu. Mohl byste mi někdo poradit co dělám špatně? Předem děkuji.
Tiskni
Sdílej:
Teď jsem to už přepsal s pomocí SimpleXML pro PHP5 a ještě potřebuji něco pro PHP4.A proc? To je dneska takovy problem sehnat hosting pro PHP5? Kdyz se budeme neustale ohlizet na PHP4, jen to bude oddalovat jeho definitivni smrt, ke ktere je urceno.
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.