convert xml data into mysql insert/update query using php

醉酒当歌 提交于 2019-12-03 09:14:39

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;

?>

You're getting back exactly what you asked for. attributes() looks at the attributes, name gives you the name of the attribute. You can get the name by doing this

$movieName = (string)$xml->column;

If you want all the values, you can loop through the columns

foreach($xml->column as $xColumn) {
  $value = (string)$xColumn;
}

the value of the name attribute IS movie_name. Titanic is the value of the Column element that has the name attribute with the value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!