Re: xml/cap problems
| From: | Adam Richardson | Date: | Mon, 23 Aug 2010 02:48:19 +0000 |
| Subject: | Re: xml/cap problems | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-307627@lists.php.net to get a copy of this message | ||
On Sat, Aug 21, 2010 at 5:53 PM, Tom Shaw <tshaw@oitc.com> wrote:
> I am trying to decode and encode nws compatible cap xml. An example is at
> the bottom of this post. simplexml_load_file works fine if "cap:" is removed
> before processing. However, if it is not simplexml_load_file does not parse
> the file at all and libxml_get_errors returns no errors.
>
> Any and all help is appreciated.
>
> Tom
>
> <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
> <cap:alert
> xmlns:cap='http://www.incident.com/cap/1.0'>
> <cap:identifier>NOAA-NWS-ALERTS National
> 2010-08-21T13:26:34-04:00</cap:identifier>
> <cap:sender>w-nws.webmaster@noaa.gov</cap:sender>
> <cap:sent>2010-08-21T13:26:34-04:00</cap:sent>
> <cap:status>Actual</cap:status>
> <cap:msgType>Alert</cap:msgType>
> <cap:scope>Public</cap:scope>
> <cap:note>
> Current Watches, Warnings and Advisories for the United States Issued by
> the National Weather Service
> </cap:note>
>
> <cap:references>http://www.weather.gov/alerts/us.html</cap:references>
> <cap:info>
> <cap:category>Met</cap:category>
> <cap:event>na</cap:event>
> <cap:urgency>Unknown</cap:urgency>
> <cap:severity>Unknown</cap:severity>
> <cap:certainty>Unknown</cap:certainty>
> <cap:headline>
> Current Watches, Warnings and Advisories for Alaska Issued by the National
> Weather Service
> </cap:headline>
>
> <cap:web>http://www.weather.gov/alerts/us.html#ak</cap:web>
> </cap:info>
> ....
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Hi Tom,
Remember, the namespace is the uri, and the prefix is merely a short-hand:
http://www.w3.org/TR/REC-xml-names/
<http://www.w3.org/TR/REC-xml-names/>When
using simple xml objects, you can
directly reference elements by using the namespace:
http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/
<http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/>However,
my preference is to use XPath capabilities, and I've processed a couple
elements on your example to demonstrate how this works:
$doc = <<<EOD
<test xmlns:cap='http://www.incident.com/cap/1.0'>
<cap:alert>
<cap:identifier>NOAA-NWS-ALERTS National
2010-08-21T13:26:34-04:00</cap:identifier>
<cap:sender>w-nws.webmaster@noaa.gov</cap:sender>
<cap:sent>2010-08-21T13:26:34-04:00</cap:sent>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:scope>Public</cap:scope>
<cap:note>
Current Watches, Warnings and Advisories for the United States Issued by the
National Weather Service
</cap:note>
<cap:references>http://www.weather.gov/alerts/us.html</cap:references>
<cap:info>
<cap:category>Met</cap:category>
<cap:event>na</cap:event>
<cap:urgency>Unknown</cap:urgency>
<cap:severity>Unknown</cap:severity>
<cap:certainty>Unknown</cap:certainty>
<cap:headline>
Current Watches, Warnings and Advisories for Alaska Issued by the National
Weather Service
</cap:headline>
<cap:web>http://www.weather.gov/alerts/us.html#ak</cap:web>
</cap:info>
</cap:alert>
</test>
EOD;
echo $doc;
$cap = simplexml_load_string($doc);
$cap->registerXPathNamespace('cap', 'http://www.incident.com/cap/1.0');
echo 'Message type:'.current($cap->xpath('//cap:msgType')).'<br
/>';
echo 'Info
category:'.current($cap->xpath('//cap:info/cap:category')).'<br
/>';
Hope this helps,
Adam
--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com