Jump to content

Reading XML with PHP


HCProfessionals

Recommended Posts

I just can't wrap my head around it, but I am trying to integrate weather into my website using NOAA.

 

1. Here is the NOAA page: http://forecast.weather.gov/MapClick.php?CityName=Mio&state=MI&site=APX&lat=44.663&lon=-84.1446

2. Here is the XML to that page: http://forecast.weather.gov/MapClick.php?lat=44.66300&lon=-84.14460&FcstType=dwml

 

Now what I want to do is to have PHP on my website that reads the XML from the NOAA site and create the "Forecast At A Glance". Possible or too challenging?

Link to comment
Share on other sites

hi,

 

Your XML contains a bit of namespaces, So if you find simpleXML difficult, you can try using a class I created. Which makes  accessing CDATA sections and namespaces easy/

 

http://crxml.pagodabox.com/

 

You can download the archive at 

http://crxml.pagodabox.com/crxml.tar

 

The advantage of this class is that you have a search function. You can call it with a name 'visibility' and it will return the php statement using this class that you can use to access the value of that node.

 

for example after loading your xml into the class , the statemment

 

var_dump($crxml->search('visibility'));

 

will output  array with 1 element.

 

array(1) {
  [0]=>
  array(7) {
    ["nodeName"]=>
    string(10) "visibility"
    ["accessStatement"]=>
    string(85) "...->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility"
    ["nodeChildrenCount"]=>
    int(1)
    ["nodeType"]=>
    int(1)
    ["namespaceURI"]=>
    NULL
    ["nodeContent"]=>
    string(52) "<visibility units="statute miles">10.00</visibility>"
    ["htmlEncodedNodeContent"]=>
    string(74) "<visibility units="statute miles">10.00</visibility>"
  }
}

 

In the above section there is a "accessStatement" element....this contains value "->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility"

 

so to access the value of that node from php you can use

 

echo $crxml->->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility;

 

will echo 10.

 

to get the 'Units' attribute for visibility  you can give as

 

echo $crxml->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility['units']

 

will echo 'statute miles'...

 

This way you dont have to manually find your way through the XML.  What ever you can do with simpleXML like iterating, You can do with this class also.

 

I have to warn you that even though I have somewhat tested and is activity developing this class, I am the only one working on this.

 

So you might want to try simplexml first. Even though it seems a bit hard to me, people have been using it for long, and there will be solutions in the net for what ever issue you come up with.

 

Or try using DOM XML functions. Its What I have used to make the above class. I Have found it much more consistent and powerful.

 

If you use DOM function, you better take a look at XPath also http://www.tizag.com/xmlTutorial/xpathtutorial.php.

 

If you choose to use simpleXML pls see the link

 

http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html

 

That is not meant to scare you. But it shows how to access xml nodes when namespaces are involved

 

Good Luck.....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.