douzhuo2002 2018-11-06 13:49
浏览 54
已采纳

使用php将Web服务中的xml数据解析到数据库中

I'm currently working on a project where i have to extract some data from an online XML web service about earthquake and insert the data into a database.

My connection seems to be working since it displays "connected successfully" in my localhost web page.

The part where the code is extracting the data location, date & time, latitude longitude and magnitude does not seem to be working as it should have been displaying the success or failure message. But nothing is being displayed for this part. The data are not being in the table in phpMyAdmin.

I added an echo to see if the file is being read till the end and it showed the message "file read complete".

output image

output image with var_dump

Here is my code:

<?php $url="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.quakeml";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_URL, $url);
   $data = curl_exec ($ch);
   curl_close($ch);
   $xml = simplexml_load_string($data);

   $db_user = 'root';
   $db_pass = '';
   $db_name = 'Earthquake';
   $db_host = 'localhost';

   $con = mysqli_connect("$db_host", "$db_user", "$db_pass", "$db_name");
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}else{
    echo "Connected successfully";
}

    //GETTING LOCATION OF EARTHQUAKE
    foreach ($xml -> description as $row ){
            $location = $row -> text;

            $sql = "INSERT INTO tbl_earthquake (Location) VALUES (location)";

            if ($con->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $con->error;
            }   }

        // GETTING DATE AND TIME OF EARTHQUAKE

       foreach ($xml -> time as $row){
            $datetime = $row -> value;

            $sql = "INSERT INTO tbl_earthquake (DateandTime) VALUES ($datetime)";

            if ($con->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $con->error;
            }
                }
    //GETTING LATITUDE OF EARTHQUAKE

     foreach ($xml -> latitude as $row){
            $latitude = $row -> value;

            $sql = "INSERT INTO tbl_earthquake (Latitude) VALUES ($latitude)";

            if ($con->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $con->error;
            }
                }
    //GETTING LONGITUDE OF EARTHQUAKE

     foreach ($xml -> longitude as $row){
            $longitude = $row -> value;

            $sql = "INSERT INTO tbl_earthquake (Longitude) VALUES ($longitude)";

            if ($con->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $con->error;
            }
                }
    //GETTING MAGNITUDE OF EARTHQUAKE

     foreach ($xml -> mag as $row){
            $magnitude = $row -> value;

            $sql = "INSERT INTO tbl_earthquake (Magnitude) VALUES ($magnitude)";

            if ($con->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
                } 
                var_dump($xml->description, $xml->time, $xml->latitude, $xml->longitude, $xml->mag);

                 echo"file read complete;"
   ?>
  • 写回答

1条回答 默认 最新

  • dongqiao1964 2018-11-06 18:44
    关注

    The XML content you are reading has two intermediate tags before you get to the relevant tags like <description>. When you check the XML you see the following tags:

    <q:quakeml xmlns="http://quakeml.org/xmlns/bed/1.2" 
               xmlns:anss="http://anss.org/xmlns/event/0.1" 
               xmlns:catalog="http://anss.org/xmlns/catalog/0.1" 
               xmlns:q="http://quakeml.org/xmlns/quakeml/1.2">
        <eventParameters publicID="quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.quakeml">
            <event catalog:datasource="us"
                   catalog:eventsource="us"
                   catalog:eventid="2000i5mj"
                   publicID="quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/us2000i5mj.quakeml">
                <description>
                    [...]
    

    You have to read these <eventParameters> and <event> tags as well. Check the whole XML code from the source and check the structure of the XML file (which tag is contained in which other tag). You might need an XML beautifier/formatter for a better view (maybe your browser has one built in) because the XML code in the source does not have line breaks, which makes it difficult to read.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图