SkaKri Posted May 27, 2006 Report Share Posted May 27, 2006 (edited) nu tad tā. pirms pāris nedēļām rakstīju, jautājumu, kā lai no xml (nu - piem rss) ieceļ datus sql datubāzē. atradu variantu, kas zin, moš' kādam noder? <?xml version='1.0' ?> <xml> <PLACE> <ID>1</ID> <NAME>nosaukums</NAME> <POSTER>nu es taču</POSTER> </PLACE> <PLACE> <ID>2</ID> <NAME>atkal nosaukums</NAME> <MORE_INFO>info!</MORE_INFO> <POSTER>atkal es.</POSTER> <URL>http://www.runcis.lv</URL> <ABBR>kaķis</ABBR> </PLACE> </xml> <?php //te vēl iemet sql konekciju, protams... class RSSParser { var $insideitem = false; var $tag = ""; var $title = ""; var $description = ""; var $link = ""; var $date = ""; function startElement($parser, $tagName, $attrs) { if ($this->insideitem) { $this->tag = $tagName; } elseif ($tagName == "PLACE") { //"PLACE" nomaini ar start tagu $this->insideitem = true; } } function endElement($parser, $tagName) { if ($tagName == "PLACE") { /* fecho('id: '.trim($this->ID).'<BR>'); //izmantoju, lai redzētu, kas pievienojas,kas ne' fecho('tips: '.htmlspecialchars(trim($this->TYPE)).'<BR>'); fecho('Nosaukums: '.htmlspecialchars(trim($this->TITLE)).'<BR>'); fecho('Datums: '.html_entity_decode(trim($this->START_DATE)).'<BR>'); */ $query = "SELECT * FROM kakumeerce WHERE `place_id`=".html_entity_decode(trim($this->ID))." LIMIT 1;"; $query_result = mysql_query ($query) or die (mysql_error()); if (mysql_num_rows($query_result)) //ja ieraksts jau eksistē (čekojam pēc ID) { //fecho ('<B><font color="990000">nepievienojam</font></B><BR><BR>'); fecho('<font color="990000">'.htmlspecialchars(trim($this->NAME)).'</font><br>'); } else { $query = "INSERT INTO kakumeerce ( //metam iekšā db! `place_id`, `place_name`, `place_adress`, `place_map_url`, `poster`, `place_url`, `place_abbr`) VALUES ( '".addslashes(htmlspecialchars(trim($this->ID)))."', '".addslashes(htmlspecialchars(trim($this->NAME)))."', '".addslashes(htmlspecialchars(trim($this->MORE_INFO)))."', '".addslashes(htmlspecialchars(trim($this->MAP_URL)))."', '".addslashes(htmlspecialchars(trim($this->POSTER)))."', '".addslashes(htmlspecialchars(trim($this->URL)))."', '".addslashes(htmlspecialchars(trim($this->ABBR)))."');"; //echo $query; $result = mysql_query ($query) or die (mysql_error()); fecho(htmlspecialchars(trim($this->NAME)).'</font><br>'); /* fecho('id: '.trim($this->ID).'<BR>'); fecho('tips: '.htmlspecialchars(trim($this->TYPE)).'<BR>'); fecho('Nosaukums: '.htmlspecialchars(trim($this->TITLE)).'<BR>'); fecho('Datums: '.html_entity_decode(trim($this->START_DATE)).'<BR>'); */ } //un te definējam, ko mums vajadzētu parsēt... $this->ID = ""; $this->NAME = ""; $this->MORE_INFO = ""; $this->MAP_URL = ""; $this->URL = ""; $this->POSTER = ""; $this->ABBR = ""; $this->URL = ""; $this->insideitem = false; } } function characterData($parser, $data) { if ($this->insideitem) { switch ($this->tag) { //un te definējam, ko mums vajadzētu parsēt... atkal. case "ID": $this->ID .= $data; break; case "NAME": $this->NAME .= $data; break; case "MORE_INFO": $this->MORE_INFO .= $data; break; case "MAP_URL": $this->MAP_URL .= $data; break; case "URL": $this->URL .= $data; break; case "POSTER": $this->POSTER .= $data; break; case "ABBR": $this->ABBR .= $data; break; case "URL": $this->URL .= $data; break; } } } } $xml_parser = xml_parser_create(); $rss_parser = new RSSParser(); xml_set_object($xml_parser,$rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://muusujaukaalapa.lv/rss/kakjis.xml","r") //norādi adresi protams. or die("Nevar nolasīt RSS datus."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("Nevar piekonektēties serverim, lai saņemtu datus.<br />XML kļūda: %s rindā %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?> palabojiet, ja kur maldos, vai kā uzlabot. varētu arī piemest pie 'foruma kartes'. oi! btw - ērti izmantot kā cron job, ja atslēdzam teksta izvadi. Edited May 27, 2006 by SkaKri Link to comment Share on other sites More sharing options...
SkaKri Posted June 16, 2006 Author Report Share Posted June 16, 2006 btw - ja rss ir ar ?meesls=o-jā, tad ērti lasīt ar curl. Link to comment Share on other sites More sharing options...
Recommended Posts