怎样将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

sqlserver2005分解并导入xml文件[@more@]

1.

一次性导入:

declare

@idoc

int

declare

@doc

xml

select

@doc=bulkcolumn

from

openrowset(bulk

n'e:mstarindustrycodes.xml',

single_blob)

as

x

exec

sp_xml_preparedocument

@idoc

output,

@doc

select

*

into

tmp_tab

from

openxml

(@idoc,

'/root/record'/',2)

with

(

industrycode

varchar(10)

,industryglobalid

varchar(10)

,industryname

varchar(100)

,sectorcode

varchar(10)

,sectorglobalid

varchar(10)

,sectorname

varchar(100)

,supersectorcode

varchar(10)

,supersectorname

varchar(100)

,groupcode

varchar(10)

,groupname

varchar(100)

,countryid

varchar(3)

)

exec

sp_xml_removedocument

@idoc

select

*

from

tmp_tab

2.

先导入到表中varchar(max)列,然后再用openxml解析,读出。

--

使用single_clob参数,tmp_raw中字段为varcahr(max)类型

select

*

into

tmp_raw

from

openrowset(bulk

n'e:mstarindustrycodes.xml',

single_clob)

as

x

declare

@idoc

int

declare

@doc

xml

select

@doc

=

bulkcolumn

from

tmp_raw

exec

sp_xml_preparedocument

@idoc

output,

@doc

select

top

10

*

from

openxml

(@idoc,

'/root/record',

1)

with

(

industrycode

varchar(10)

,industryglobalid

varchar(10)

,industryname

varchar(100)

,sectorcode

varchar(10)

,sectorglobalid

varchar(10)

,sectorname

varchar(100)

,supersectorcode

varchar(10)

,supersectorname

varchar(100)

,groupcode

varchar(10)

,groupname

varchar(100)

,countryid

varchar(3)

)

exec

sp_xml_removedocument

@idoc


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存