简体   繁体   中英

Extract data from multi level tagged xml

I have a problem with retrieving data from multi tagged xml file. Here is a example of xml:

 <forecast5day>
     <city>Өлгий</city>
     <data>
     <weather>
      <date>2018-10-27</date>
      <temperatureNight>-7</temperatureNight>
      <temperatureDay>7</temperatureDay>
      <phenoIdNight>7</phenoIdNight>
      <phenoNight>Багавтар үүлтэй</phenoNight>
      <phenoIdDay>5</phenoIdDay>
      <phenoDay>Багавтар үүлтэй</phenoDay>
      <windNight>5</windNight>
      <windDay>6</windDay>
     </weather>
    </forecast5day>

And my php example

<?php
    include 'includes/config.php'; // Initialize db;
    $url = "http://tsag-agaar.gov.mn/forecast_xml";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    // Getting data
    $data = curl_exec($ch);
    curl_close($ch);

    global $conn; // Creating connection

    $xml = simplexml_load_string($data);

    foreach ($xml -> forecast5day as $row) {
        var_dump($xml-> forest5day);
        $city = $row -> city;
        $date = $row -> date;
        $temperatureNight = $row -> temperatureNight;
        $temperatureDay = $row -> temperatureDay;
        $phenoIdNight = $row -> phenoIdNight;
        $phenoNight = $row -> phenoNight;
        $phenoIdDay = $row -> phenoIdDay;
        $phenoDay = $row -> phenoDay;
        $windNight = $row -> windNight;
        $windDay = $row -> windDay;
        $sql = "INSERT INTO weather(city,wdate,temperatureNight,temperatureDay,phenoIdNight,phenoNight,phenoIdDay,phenoDay,windNight,windDay) VALUES ('{$city}','{$date}','{$temperatureNight}','{$temperatureDay}','{$phenoIdNight}','{$phenoNight}','{$phenoIdDay}','{$phenoDay}','{$windNight}','{$windDay}')";


        $result = $conn -> query($sql);
        if(!$result) {
            echo "MYSQL Error: $sql <br/>";
        } else {
            echo "SUCCES: $sql <br/>";
        }
    }
?>

And above snippet gives me:

INSERT INTO weather(city,wdate,temperatureNight,temperatureDay,phenoIdNight,phenoNight,phenoIdDay,phenoDay,windNight,windDay) VALUES ('Өлгий','','','','','','','','','')

What did i do wrong ? Its something i cannot get a other values except city . Any advice ?

For a start, it isn't valid XML. There's no closing </data> tag. However that may just be your post and the code is fine. The structure suggests the following will work:

$temperatureDay = $row->data->weather->temperatureDay;

When there are multiple weather elements, weather becomes an array.

foreach ($row->data->weather as $weather) {
    $temperatureDay = $weather->temperatureDay;
    // and the other values
}

Are you missing a </data> tag from the example XML?

As the information you are trying to read from 'date', 'temperature' etc. is nested within a 'weather' tag, you need to drill down a level into the 'row':

$row->weather[0]->date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM