Jump to content

Parsing XML with namespaces


Texan78

Recommended Posts

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>[email protected]</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

Link to comment
https://forums.phpfreaks.com/topic/277742-parsing-xml-with-namespaces/
Share on other sites

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).

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.