怎样将XML文件导入数据库

怎样将XML文件导入数据库,第1张

要用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

var tmpquery2:tadoquery

tmpfile:string

tmptable:tadotable

begin

if application.MessageBox( '导入答案将会清空以前的考生答案,确定是否继续? ', '提醒 ',MB_OKCANCEL or MB_ICONWARNING)=mrok then

begin

IF BSopen.Execute then

try

try

begin

tmpfile:=bsopen.FileName

tmptable:=tadotable.Create(self)

tmpquery2:=tadoquery.Create (self)

tmpquery2.Connection :=dm.connect

tmptable.Connection :=dm.connect

tmptable.TableName:= 'exam_answer '

with tmpquery2 do

begin

close

sql.Clear

sql.Add( 'delete * from exam_answer ')

execsql

end

tmptable.LoadFromFile(tmpfile)

tmptable.First

while not tmptable.Eof do

begin

with tmpquery2 do

begin

close

sql.Clear

sql.Add( 'insert into exam_answer(kh,xm,sj_id,sjts,tk_id,stbh,tx,daa,dab,dac,dad,dae,daf,tkda,df) values(:kh,:xm,:sj_id,:sjts,:tk_id,:stbh,:tx,:daa,:dab,:dac,:dad,:dae,:daf,:tkda,:df) ')

parameters.ParamByName( 'kh ').Value :=tmptable.fieldbyname( 'kh ').AsString

parameters.ParamByName( 'xm ').Value :=tmptable.fieldbyname( 'xm ').AsString

parameters.ParamByName( 'sj_id ').Value :=tmptable.fieldbyname( 'sj_id ').AsString

parameters.ParamByName( 'tx ').Value :=tmptable.fieldbyname( 'tx ').AsString

parameters.ParamByName( 'sjts ').Value :=tmptable.fieldbyname( 'sjts ').AsString

parameters.ParamByName( 'tk_id ').Value :=tmptable.fieldbyname( 'tk_id ').AsString

parameters.ParamByName( 'stbh ').Value :=tmptable.fieldbyname( 'stbh ').AsString

parameters.ParamByName( 'daa ').Value :=tmptable.fieldbyname( 'daa ').AsBoolean

parameters.ParamByName( 'dab ').Value :=tmptable.fieldbyname( 'dab ').asboolean

parameters.ParamByName( 'dac ').Value :=tmptable.fieldbyname( 'dac ').AsBoolean

parameters.ParamByName( 'dad ').Value :=tmptable.fieldbyname( 'dad ').AsBoolean

parameters.ParamByName( 'dae ').Value :=tmptable.fieldbyname( 'dae ').AsBoolean

parameters.ParamByName( 'daf ').Value :=tmptable.fieldbyname( 'daf ').AsBoolean

parameters.ParamByName( 'tkda ').Value :=tmptable.fieldbyname( 'tkda ').AsString

parameters.ParamByName( 'df ').Value :=tmptable.fieldbyname( 'df ').AsString

execsql

end

tmptable.next

end

showmessage( '导入完成,累死了... ')

end

finally

tmptable.Free

tmpquery2.Free

end

except

showmessage( '导入未完成。:( ')

end

end

end

如果单纯的LOAD出来,它是不直接写到后台数据表的。分析下源码就知道它LOAD之前先CLOSE了数据表。只有一条一条增加到后台数据表了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9729520.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存