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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,