怎样将XML文件导入MYSQL
2013-06-05
展开全部
要用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;
我在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;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-05
展开全部
可以使用MYSQL的中文管理工具导入啊,有个汇入精灵,支持大概30多种格式的导入。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询