0

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 ?

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

But if i had multiple weather tag in one data tag ? how to get it ?
I'd need to see the XML
is it inside that root loop ?
I would guess so!
in my db it just inserting one row.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.