dp815292 2015-08-12 10:42
浏览 41

用于插入查询的PHP XML数据

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

  • 写回答

1条回答 默认 最新

  • dso0139 2015-08-13 04:35
    关注

    There is error in your query

    Change from

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

    To

    $query = " INSERT INTO  $table ( $columns ) VALUES ( $values ) ";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)