Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [HTML][XML][PHP] Pobieranie danych z xml do pliku html
brzanek
post 18.10.2013, 09:50:18
Post #1





Grupa: Zarejestrowani
Postów: 429
Pomógł: 0
Dołączył: 8.11.2012

Ostrzeżenie: (0%)
-----


Witam chciał bym uzyskać prognozę pogody na swojej stronie. Serwis http://www.openweathermap.org/API udostępnia prognozę na kilka dni do przodu uzyskałem kod xml ale nie mogę sobie z nim poradzić.
  1. <weatherdata><location><name>Choszczno</name><type/><country>PL</country><timezone/><location altitude="0" latitude="53.169048" longitude="15.42054" geobase="geonames" geobaseid="0"/></location><credit/><meta><lastupdate>2013-10-18T08:24:19</lastupdate><calctime>0.0175</calctime><nextupdate>2013-10-18T11:24:19</nextupdate></meta><sun rise="2013-10-18T05:31:35" set="2013-10-18T15:55:07"/><forecast><time day="2013-10-18"><symbol number="500" name="light rain" var="10d"/><precipitation value="1" type="rain"/><windDirection deg="287" code="WNW" name="West-northwest"/><windSpeed mps="6.9" name="Moderate breeze"/><temperature day="10.91" min="5.07" max="11.54" night="5.07" eve="8.65" morn="10.23"/><pressure unit="hPa" value="1016.67"/><humidity value="98" unit="%"/><clouds value="overcast clouds" all="92" unit="%"/></time><time day="2013-10-19"><symbol number="802" name="scattered clouds" var="03d"/><precipitation/><windDirection deg="122" code="ESE" name="East-southeast"/><windSpeed mps="3.81" name="Gentle Breeze"/><temperature day="7.76" min="0.49" max="10.39" night="9.63" eve="9.13" morn="1.21"/><pressure unit="hPa" value="1027.64"/><humidity value="89" unit="%"/><clouds value="scattered clouds" all="36" unit="%"/></time><time day="2013-10-20"><symbol number="804" name="overcast clouds" var="04d"/><precipitation/><windDirection deg="207" code="SSW" name="South-southwest"/><windSpeed mps="5.55" name="Moderate breeze"/><temperature day="13.39" min="10.32" max="17.02" night="15.51" eve="17.02" morn="10.32"/><pressure unit="hPa" value="1015.07"/><humidity value="95" unit="%"/><clouds value="overcast clouds" all="92" unit="%"/></time><time day="2013-10-21"><symbol number="500" name="light rain" var="10d"/><precipitation value="2.47" type="rain"/><windDirection deg="178" code="S" name="South"/><windSpeed mps="1.95" name="Light breeze"/><temperature day="16.45" min="11.42" max="16.45" night="12.17" eve="16.45" morn="11.42"/><pressure unit="hPa" value="1020.09"/><humidity value="0" unit="%"/><clouds value="broken clouds" all="79" unit="%"/></time><time day="2013-10-22"><symbol number="501" name="moderate rain" var="10d"/><precipitation value="11.04" type="rain"/><windDirection deg="259" code="W" name="West"/><windSpeed mps="9" name="Fresh Breeze"/><temperature day="15.73" min="13.36" max="15.73" night="13.36" eve="13.72" morn="14.16"/><pressure unit="hPa" value="1016.35"/><humidity value="0" unit="%"/><clouds value="overcast clouds" all="93" unit="%"/></time><time day="2013-10-23"><symbol number="500" name="light rain" var="10d"/><precipitation value="1.01" type="rain"/><windDirection deg="240" code="WSW" name="West-southwest"/><windSpeed mps="7.58" name="Moderate breeze"/><temperature day="16.31" min="12.71" max="16.31" night="14.49" eve="15.1" morn="12.71"/><pressure unit="hPa" value="1014.07"/><humidity value="0" unit="%"/><clouds value="broken clouds" all="63" unit="%"/></time><time day="2013-10-24"><symbol number="500" name="light rain" var="10d"/><precipitation value="1.54" type="rain"/><windDirection deg="237" code="WSW" name="West-southwest"/><windSpeed mps="5.14" name="Gentle Breeze"/><temperature day="14.74" min="13.08" max="14.74" night="13.08" eve="14.74" morn="14.49"/><pressure unit="hPa" value="1016.06"/><humidity value="0" unit="%"/><clouds value="broken clouds" all="72" unit="%"/></time></forecast></weatherdata>


Proszę o pomoc jak zapisać to i wyświetlić w pliku html lub php. Jedynie co udało mi się wydobyć z kodu xml to to http://serwer1323572.home.pl/1/po.php czyli miejscowość i datę.
Z góry dziękuję za pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
nospor
post 18.10.2013, 09:52:45
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Uzyj simpleXML lub czegos podobnego do operowania na XMLach


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
brzanek
post 18.10.2013, 09:55:12
Post #3





Grupa: Zarejestrowani
Postów: 429
Pomógł: 0
Dołączył: 8.11.2012

Ostrzeżenie: (0%)
-----


Mam taki kod.
  1. <?php
  2. $xml_load = simplexml_load_file('http://api.openweathermap.org/data/2.5/forecast/daily?q=Choszczno&mode=xml&units=metric&cnt=7');
  3. $name = $xml_load->location->name;
  4. $lastupdate = $xml_load->meta->lastupdate;
  5. $temperature_day = $xml_load->weatherdata->forecast->time_day->temperature_day;
  6.  
  7. echo "<table border='0'>
  8. <tr>
  9. <td>Miejscowość - $name</td>
  10. </tr>
  11. <tr>
  12. <td>Data - $lastupdate</td>
  13. </tr>
  14. <tr>
  15. <td>$temperature_day</td>
  16. </tr>
  17. <tr>
  18. <td></td>
  19. </tr>
  20. </table>\n";
  21. ?>


Ale temperatura już się nie wyświetla
Go to the top of the page
+Quote Post
nospor
post 18.10.2013, 10:03:58
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




1) Czemu prze meta czy location pomijasz juz weatherdata ale przy foreacast nie pomijasz juz weatherdata ? Badz konsekwentny
2) time_day->temperature_day;
Jeden z nas jest slepy, ale ja w tym xml nie widze nigdzie time_day ani temperature_day....
Owszem, widze za to time oraz temperature, zas day to atrybut a nie nazwa znacznika


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
brzanek
post 18.10.2013, 10:07:45
Post #5





Grupa: Zarejestrowani
Postów: 429
Pomógł: 0
Dołączył: 8.11.2012

Ostrzeżenie: (0%)
-----


  1. <?php
  2. $xml_load = simplexml_load_file('http://api.openweathermap.org/data/2.5/forecast/daily?q=Choszczno&mode=xml&units=metric&cnt=7');
  3. $name = $xml_load->location->name;
  4. $lastupdate = $xml_load->meta->lastupdate;
  5. $temperature = $xml_load->forecast->time->temperature;
  6.  
  7. echo "<table border='0'>
  8. <tr>
  9. <td>Miejscowość - $name</td>
  10. </tr>
  11. <tr>
  12. <td>Data - $lastupdate</td>
  13. </tr>
  14. <tr>
  15. <td>$temperature</td>
  16. </tr>
  17. <tr>
  18. <td></td>
  19. </tr>
  20. </table>\n";
  21. ?>


W ten sposób też temperatura nie wyświetla się. Co robię nie tak?
Go to the top of the page
+Quote Post
nospor
post 18.10.2013, 10:11:24
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




temparature nie ma wartosci jako znacznik. Jej wartosci podane sa w atrybutach day. min, ... itd. Masz wiec je pobrac z atrybutow. Doczytaj w simpleXML jak sie dobeirac do atrybutow.
Poza tym time jest wiele. Masz wiec po nich przeleciec pętlą


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
brzanek
post 18.10.2013, 10:22:10
Post #7





Grupa: Zarejestrowani
Postów: 429
Pomógł: 0
Dołączył: 8.11.2012

Ostrzeżenie: (0%)
-----


Nie bardzo mogę to zrozumieć. Możesz mi napisać konkretniej jak to zrobić?
Wiem co masz na myśli ale nie wiem jak utwożyć pętlę i jak dostać się do tych atrybutów. Znalazłem kilka stron o tym ale jakoś tego nie czaję.
Go to the top of the page
+Quote Post
nospor
post 18.10.2013, 10:23:00
Post #8





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Przyklady sa w dokumentacji simpleXML.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
brzanek
post 18.10.2013, 10:37:53
Post #9





Grupa: Zarejestrowani
Postów: 429
Pomógł: 0
Dołączył: 8.11.2012

Ostrzeżenie: (0%)
-----


Dodałem to
  1. foreach ($weatherdata->forecast->$time) {
  2. echo "windSpeed: ".$time["windSpeed"];
  3. echo "temperature: ".$time->temperature;
  4. echo "pressure: ".$time->pressure;
  5. }

Ale teraz jest błąd
Co jest żle?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 07:36