I have following xml data printed as array in php so now I want to convert this XML data and make insert query so how to do that as I am not so good with XML so if anyone out there who can write the sample code to convert this XML array into columns and data as part of sql query.
SimpleXMLElement Object
( [request] => SimpleXMLElement Object ( [type] => City [query] => Auckland, New Zealand )
[weather] => Array
(
[0] => SimpleXMLElement Object
(
[date] => 2015-07-01
[astronomy] => SimpleXMLElement Object
(
[sunrise] => 07:35 AM
[sunset] => 05:15 PM
[moonrise] => 04:37 PM
[moonset] => 06:07 AM
)
[maxtempC] => 15
[maxtempF] => 60
[mintempC] => 10
[mintempF] => 50
[uvIndex] => 0
Now here is php code which produce the above print_r($xml);
PHP code:
$xml_response = file_get_contents($premiumurl);
$xml = simplexml_load_string($xml_response);
Now, if anyone can write further code and make one insert query with columns and rows....it will be really great and yes I tried many solution and even looked at some tutorial but couldn;t learn or resolve this. Thank you in advanced.
SQL query in php will e something like following just writing it so someone get idea:
$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;
Thank you guys :) in advanced