homepage Welcome to WebmasterWorld Guest from 107.21.163.227
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Request assitance in parsing XML file using PHP
Parsing XML file using PHP
shadowdesign




msg:4375591
 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>

 

httpwebwitch




msg:4375600
 8:56 pm on Oct 17, 2011 (gmt 0)

Have you tried the SimpleXML class for PHP?

[php.net...]

shadowdesign




msg:4375716
 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.

httpwebwitch




msg:4376100
 6:46 pm on Oct 18, 2011 (gmt 0)

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

don't the <temperature> nodes stay grouped?

shadowdesign




msg:4376139
 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];
}

httpwebwitch




msg:4376150
 7:57 pm on Oct 18, 2011 (gmt 0)

Nice! Regex to the rescue! Glad you solved it

shadowdesign




msg:4376201
 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved