note 96494 deleted from simplexmlelement.getDocNamespaces by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Jan 2011 14:35:04 +0000 |
| Subject: | note 96494 deleted from simplexmlelement.getDocNamespaces by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176476@lists.php.net to get a copy of this message | ||
Note Submitter: vpopov at neterra dot net
----
I needed to parse RSS from Google maps. The essential thing was to get the coordinates of the points
on the map. Because Google are using namespaces, I will show you how to use SimpleXML with
namespaces.
So we have map.xml with this content:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<channel>
<link>http://maps.google.com</link>
<title>My points on map</title>
<description><![CDATA[Description:
My points on the map are so cool :)
]]></description>
<item>
<guid isPermaLink="false">00047fcda8f1020668126</guid>
<pubDate>Wed, 17 Feb 2010 15:39:46 +0000</pubDate>
<title>My first point</title>
<description><![CDATA[<div dir="ltr">5, Saint Sofia
Street</div>]]></description>
<author>me</author>
<georss:point>
42.696480 23.319941
</georss:point>
<georss:elev>0.000000</georss:elev>
</item>
<item>
<guid isPermaLink="false">00047fcd5905cbd8cc0c3</guid>
<pubDate>Wed, 17 Feb 2010 15:17:25 +0000</pubDate>
<title>My second point</title>
<description><![CDATA[<div dir="ltr">12-14, Bulgaria
bul.</div>]]></description>
<author>me</author>
<georss:point>
42.678051 23.299726
</georss:point>
<georss:elev>0.000000</georss:elev>
</item>
</channel>
</rss>
And how we parse this file:
<?php
$feed = file_get_contents("map.xml");
$xml = new SimpleXmlElement($feed);
foreach ($xml->channel->item as $entry){
echo $entry->title.'<br />';
echo strip_tags($entry->description).'<br />';
//Use namespaces
$namespaces = $entry->getNameSpaces(true);
//Get the name space that we need
$georss = $entry->children($namespaces['georss']);
//print the namspace items
echo $georss->point.'<br />';
echo $georss->elev.'<br /><br />';
}
?>
Enjoy!