homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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

 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"


<parameters applicable-location="point1">

<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
<name>Daily Maximum Temperature</name>

<temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n7-2">
<name>Daily Minimum Temperature</name>




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

Have you tried the SimpleXML class for PHP?



 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)

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

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)

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.

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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved