2010-11-09 145 views
2

我從原來的鏈接中發現這個: http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html#dev 如何將xml保存到mysql數據庫?

<?php 
    function StartElement($parser, $name, $attrs) { 
     if (!empty($attrs['RATE'])) { 
      echo "1&euro;=".$attrs['RATE']." ".$attrs['CURRENCY']."<br />"; 
     } 
    } 
    $xml_parser= xml_parser_create(); 
    xml_set_element_handler($xml_parser, "StartElement", ""); 
    // for the following command you will need file_get_contents (PHP >= 4.3.0) 
    // and the config option allow_url_fopen=On (default) 
    xml_parse($xml_parser, file_get_contents ("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")); 
    xml_parser_free($xml_parser); 
?> 

然後將看起來像:

INSERT INTO `curr_table` (`curr_id`, `curr_title`, `Code`, `decimal_place`, `value`) VALUES 
(1, 'EURO', 'EUR', '2', 1.3917), 
(2, 'Japan Yen', 'JPY', '2', 112.88), 
(3, 'Bla..bla', 'BGN', '2', 1.9558), 
..............................etc 
(20, 'Bla..bla.', 'CZK', '2', 24.575); 

任何指針和樣本代碼將被讚賞和感謝提前

+0

這是2010年,使用SimpleXML的或至少DOM – Phil 2010-11-09 04:16:59

+0

有一個我可以學習的鏈接?我對此很陌生。 TQ – user485783 2010-11-09 15:22:39

+1

@ user485783 http://php.net/manual/en/book.simplexml.php – Phil 2010-11-09 23:53:04

回答