将xml数据转换为mysql插入/更新查询使用php [英] convert xml data into mysql insert/update query using php

查看:155
本文介绍了将xml数据转换为mysql插入/更新查询使用php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是sample.xml文件:

这是一个基于xml文件读取数据的工程,

 < table name =movies> 
< column name =movie_name> Titanic< / column>
< column name =dvd> 40< / column>
< column name =blueray> 5< / column>
< column name =netflix> 4< / column>
< column name =amazon> 3< / column>
< / table>

我将问题分解为:
1)从XML文件获取值。
2)将提取的值插入到mysql数据库中。



我能够将值插入数据库,但是硬部分是从



这是我的数据库表看起来像:



数据库名称:Movies
列:movie_name,dvd,blueray,netflix,amazon



下面是我试图从xml中提取属性值的代码。

 <?php 
$ source ='sample.xml';
// load as string
$ xml = new SimpleXMLElement($ source,null,true);
$ movie_name = $ xml-> column-> attributes() - > name;
echo $ movie_name。 \\\
;
?>

输出:
不是获取电影Titanic的名字, 。

解决方案

您可以使用此查询:

 <?php 
$ source ='sample.xml';
// load as string
$ xml = new SimpleXMLElement($ source,null,true);
$ movie_name = $ xml-> column-> attributes() - > name;

// echo $ movie_name。 \\\
;

$ table = $ xml-> attributes() - > name;

$ columns =;
foreach($ xml as $ column){
$ columns。=(string)$ column-> attributes() - > name。
}
$ columns = rtrim($ columns,,);

$ values =;
foreach($ xml-> column as $ value){
$ values。= $ value。,;
}
$ values = rtrim($ values,,);

$ query =INSERTO INTO $ table($ columns)VALUES($ values);

echo $ query;

?>


I am working on this project that basically reads the data from xml file and insert/update the data into mysql database.

Here is the sample.xml file:

<table name="movies">
    <column name="movie_name">Titanic</column>
    <column name="dvd">40</column>
    <column name="blueray">5</column>
    <column name="netflix">4</column>
    <column name="amazon">3</column>
</table>

I break down the problem into: 1) Get the values from XML file. 2) Insert the extracted values into mysql database.

I am able to work on insert the values to database, but the hard part is getting the values from xml making me crazy.

Here is my Database Table looks like:

Database name: Movies Columns: movie_name, dvd, blueray, netflix, amazon

Here is the code that I tried to extract the attribute values from xml.

<?php
$source = 'sample.xml';
// load as string
$xml = new SimpleXMLElement($source,null,true);
$movie_name= $xml->column->attributes()->name;
echo $movie_name. "\n";
?>

Output: Instead of getting the name of movie "Titanic", I get "movie_name".

解决方案

You can use this query:

<?php
$source = 'sample.xml';
// load as string
$xml = new SimpleXMLElement($source,null,true);
$movie_name= $xml->column->attributes()->name;

//echo $movie_name. "\n";

$table = $xml->attributes()->name;    

    $columns ="";
    foreach($xml as $column){
        $columns .= (string)$column->attributes()->name .",";
    }
    $columns = rtrim($columns, ",");

    $values ="";
    foreach($xml->column as $value){
        $values .= $value ."," ;
    }
    $values = rtrim($values, ",");

$query = " INSERTO INTO  $table ( $columns ) VALUES ( $values ) ";   

echo $query;

?>

这篇关于将xml数据转换为mysql插入/更新查询使用php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆