我在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
举例说明如下xml文件名为: text.xml
xml数据文件的结构如下:
<node1>
<node2 name="abc">123</node2>
</node1>
sql命令如下:
SET @xml = LOAD_FILE('text.xml')-- 要指定完整的文件位置
SELECT ExtractValue(@xml, '/node1/node2/@name') as name,ExtractValue(@xml, '/node1/node2') as data
返回数据结果就是:
name | data
abc | 123
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)