如何将xml保存到mysql数据库?

时间:2010-11-09 04:09:37

标签: sql xml database parsing

我从原始链接中找到了这个: http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html#dev e.g。

<?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);

任何指针和示例代码都会受到赞赏并提前感谢

1 个答案:

答案 0 :(得分:0)

如果您使用的是MySQL 5.1或更高版本,则需要XML支持:

http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html#xml-5.1-in-and-out

相关问题