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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题