Welcome to WebmasterWorld Guest from 54.158.65.139

Forum Moderators: httpwebwitch

Request assitance in parsing XML file using PHP

Parsing XML file using PHP

   
8:34 pm on Oct 17, 2011 (gmt 0)


I have been doing PHP for years. I am having a bit of difficulty trying to parse this XML file (below is a sample). Usually, I do not run into any problems, however I have not dealt trying to single out the "value" in each temperature.

For the past few years I had been using the Data Feed from Weather.com. That feed I parsed, however the feed from the NWS is a little different.

Unfortunately, I need to switch the weather from them to using the NWS feed.

I desire to display each value for maximum and minimum. I request any assist anyone can offer. This will give me the information I can proceed with. I am not proud to accept anyone's parsing ideas for the NWS feed.

<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWMLgen/sch\
ema/DWML.xsd">

<data>

<parameters applicable-location="point1">

<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
<name>Daily Maximum Temperature</name>
<value>80</value>
<value>82</value>
<value>78</value>
<value>75</value>
<value>78</value>
<value>76</value>
<value>77</value>
</temperature>

<temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n7-2">
<name>Daily Minimum Temperature</name>
<value>57</value>
<value>54</value>
<value>55</value>
<value>52</value>
<value>53</value>
<value>52</value>
<value>51</value>
</temperature>

</parameters>
</data>
</dwml>
8:56 pm on Oct 17, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Have you tried the SimpleXML class for PHP?

[php.net...]
2:49 am on Oct 18, 2011 (gmt 0)



Unfortunately, I tried SimpleXML however I have been unable to get the data into groups like maximum temps and minimum temps. I am trying to separate into groups.
6:46 pm on Oct 18, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



parse the XML with SimpleXML, and then var_dump() it or print_r() it.

don't the <temperature> nodes stay grouped?
7:32 pm on Oct 18, 2011 (gmt 0)



I lucked out. Somebody showed me a whole different approach in extracting the data from the XML file. Note: It works! It goes like this:

You need to set each 'section' to a variable, then parse each value out
like...

if (preg_match('/<temperature type=\"maximum\".+?>(.+?)<\/temperature>/s' , $data, $m)){
$maxtemps = $m[1];
}

if (preg_match_all('/<value>(-?[\d]+)<\/value>/s', $maxtemps,$mm)){
for ($i=0; $i < count($mm[0]); $i++){

$maxt[$i] = $mm[1][$i];
}
7:57 pm on Oct 18, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Nice! Regex to the rescue! Glad you solved it
8:59 pm on Oct 18, 2011 (gmt 0)



I love learning new approaches in solving, especially when the standard and typical approaches do not do what you desire. I love this unique approach.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month