Texan78 Posted May 7, 2013 Share Posted May 7, 2013 (edited) Hello, I have a small problem and I have been searching for hours. While I have found many results. I really haven't found one that works with the code I am currently using or I am just not knowledgeable enough about it. I am using SimpleXML to parse a XML feed. I have never used SimpleXML so I decided on this current script I would give it a shot. This is the structure of the XML I am working with below. The elements with the namespaces I am needing is <cap:event>, <cap:effective>, <cap:expires>, <cap:severity> <entry> <id>http://alerts.weather.gov/cap/wwacapget.php?x=TX124EFFB832F0.SpecialWeatherStatement.124EFFB84164TX.LUBSPSLUB.ac20a1425c958f66dc159baea2f9e672</id> <updated>2013-05-06T20:08:00-05:00</updated> <published>2013-05-06T20:08:00-05:00</published> <author> <name>w-nws.webmaster@noaa.gov</name> </author> <title>Special Weather Statement issued May 06 at 8:08PM CDT by NWS</title> <link href="http://alerts.weather.gov/cap/wwacapget.php?x=TX124EFFB832F0.SpecialWeatherStatement.124EFFB84164TX.LUBSPSLUB.ac20a1425c958f66dc159baea2f9e672"/> <summary>...SIGNIFICANT WEATHER ADVISORY FOR COCHRAN AND BAILEY COUNTIES... AT 808 PM CDT...NATIONAL WEATHER SERVICE DOPPLER RADAR INDICATED A STRONG THUNDERSTORM 30 MILES NORTHWEST OF MORTON...MOVING SOUTHEAST AT 25 MPH. NICKEL SIZE HAIL...WINDS SPEEDS UP TO 40 MPH...CONTINUOUS CLOUD TO GROUND LIGHTNING...AND BRIEF MODERATE DOWNPOURS ARE POSSIBLE WITH</summary> <cap:event>Special Weather Statement</cap:event> <cap:effective>2013-05-06T20:08:00-05:00</cap:effective> <cap:expires>2013-05-06T20:45:00-05:00</cap:expires> <cap:status>Actual</cap:status> <cap:msgType>Alert</cap:msgType> <cap:category>Met</cap:category> <cap:urgency>Expected</cap:urgency> <cap:severity>Minor</cap:severity> <cap:certainty>Observed</cap:certainty> <cap:areaDesc>Bailey; Cochran</cap:areaDesc> <cap:polygon>34.19,-103.04 34.19,-103.03 33.98,-102.61 33.71,-102.61 33.63,-102.75 33.64,-103.05 34.19,-103.04</cap:polygon> <cap:geocode> <valueName>FIPS6</valueName> <value>048017 048079</value> <valueName>UGC</valueName> <value>TXZ027 TXZ033</value> </cap:geocode> <cap:parameter> <valueName>VTEC</valueName> <value></value> </cap:parameter> </entry> This is a sample of the php I am using to parse the XML and it works great for normal non-namespace elements. It does exactly what I need it to do. My question is, what do I need to do to parse the elements with namespaces cap: with the including code below? ini_set('display_errors','1'); $tdata = ""; $data = "http://alerts.weather.gov/cap/tx.php?x=1"; $entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); foreach ($result as $entry): $title = $entry->title; $summary = $entry->summary; $tdata .= "<p><strong>$title</strong></p><p>$summary</p><hr/>"; endforeach; endif; Any suggestions would be greatly appreciated as I have gotten this far and I can't seem to tackle this last small obstacle. -Thanks Edited May 7, 2013 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/277742-parsing-xml-with-namespaces/ Share on other sites More sharing options...
Solution requinix Posted May 7, 2013 Solution Share Posted May 7, 2013 With arrow -> notation, think of it like there can be only one "active" namespace at a time. children() can switch the active namespace, like with $entry->children("cap", true)->event There should be a feature request somewhere to allow variable-variable notation with namespaces, like $entry->{"cap:event"}(which doesn't work now). Quote Link to comment https://forums.phpfreaks.com/topic/277742-parsing-xml-with-namespaces/#findComment-1428804 Share on other sites More sharing options...
Texan78 Posted May 7, 2013 Author Share Posted May 7, 2013 With arrow -> notation, think of it like there can be only one "active" namespace at a time. children() can switch the active namespace, like with $entry->children("cap", true)->event<side-comment>There should be a feature request somewhere to allow variable-variable notation with namespaces, like $entry->{"cap:event"}(which doesn't work now).</side-comment> Oh my you're a saint!!! Thank you so much that works like a charm! I knew it was something simple and out of all the searching I did nothing made it that simple. Yes, I agree strongly to allow variable-variable notation with namespaces. That would simplify things tremendously! -Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/277742-parsing-xml-with-namespaces/#findComment-1428805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.