note 96494 added to simplexmlelement.getDocNamespaces
| From: | vpopov at neterra dot net | Date: | Tue, 02 Mar 2010 10:07:47 +0000 |
| Subject: | note 96494 added to simplexmlelement.getDocNamespaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166523@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 89.190.194.18
Probable Submitter: 212.73.140.219
----
Manual Page -- http://www.php.net/manual/en/simplexmlelement.getDocNamespaces.php
Edit -- https://master.php.net/note/edit/96494
Del: integrated -- https://master.php.net/note/delete/96494/integrated
Del: useless -- https://master.php.net/note/delete/96494/useless
Del: bad code -- https://master.php.net/note/delete/96494/bad+code
Del: spam -- https://master.php.net/note/delete/96494/spam
Del: non-english -- https://master.php.net/note/delete/96494/non-english
Del: in docs -- https://master.php.net/note/delete/96494/in+docs
Del: other reasons-- https://master.php.net/note/delete/96494
Reject -- https://master.php.net/note/reject/96494
Search -- https://master.php.net/manage/user-notes.php