怎样将XML文件导入数据库
发布网友
发布时间:2022-04-27 02:05
我来回答
共2个回答
懂视网
时间:2022-04-08 08:58
php操作xml并插入到数据库中
<?php
header('content-type:text/html;charset=utf-8');
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
mysql_query('SET names utf8');
$xml_array=simplexml_load_file('suggest.xml'); //将XML中的数据,读取到数组对象中
foreach($xml_array as $tmp){
$sql = "insert into `countries` (`id` ,`value`) VALUES (NULL , '".$tmp->field."')";
mysql_query($sql);
echo $tmp->field."<br>";
}
?>
版权声明:本文博客原创文章。博客,未经同意,不得转载。
php操作xml并插入到数据库中
标签:
热心网友
时间:2022-04-08 06:06
要用php 把XML数据导入mysql ,xml里所有的数据导入数据库
我在php手册里找了个例子
to import xml into mysql
$file = "article_2_3032005467.xml";
$feed = array();
$key = "";
$info = "";
function startElement($xml_parser, $attrs ) {
global $feed;
}
function endElement($xml_parser, $name) {
global $feed, $info;
$key = $name;
$feed[$key] = $info;
$info = ""; }
function charData($xml_parser, $data ) {
global $info;
$info .= $data; }
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData" );
$fp = fopen($file, "r");
while ($data = fread($fp, 8192))
!xml_parse($xml_parser, $data, feof($fp));
xml_parser_free($xml_parser);
$sql= "INSERT INTO `article` ( `";
$j=0;
$i=count($feed);
foreach( $feed as $assoc_index => $value )
{
$j++;
$sql.= strtolower($assoc_index);
if($i>$j) $sql.= "` , `";
if($i <=$j) {$sql.= "` ) VALUES ('";}
}
$h=0;
foreach( $feed as $assoc_index => $value )
{
$h++;
$sql.= utf8_decode(trim(addslashes($value)));
if($i-1>$h) $sql.= "', '";
if($i <=$h) $sql.= "','')";
}
$sql=trim($sql);
echo $sql;