如何使用php从xml提要中提取georss:point标记

时间:2015-03-31 06:57:32

标签: xml rss simplexml

我在这个网站上看到很多链接。其中一个非常接近: parsing a georss namespace with simplexml

出于我的目的,我编辑了这样的代码:

<html>
	<head>
		<title>Testing</title>
	</head>
	<body>
	<?php
    $file = "http://www.ubalert.com/alerts.rss";
    $xml = simplexml_load_file($file);  
    $loc = $xml->channel->entry;  
    foreach ($loc->children('http://www.georss.org/georss') as $geo)
	{ 
        echo $geo;
    } 
	?>
	</body>
</html>

我想从给定的rss feed中提取georss:point tag值。我尝试了很多但失败了。运行上面的代码会产生致命的错误:

致命错误:在第10行的C:\ xampp \ htdocs \ phpproj \ Test2.php中调用null上的成员函数children()

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

我知道这是旧的,但这可能会帮助那些想要解析GeoRss Feeds或任何使用命名空间的xml的人。

$url = 'http://www.ubalert.com/alerts.rss';
$xml = simplexml_load_file($url); 
$namespaces = $xml;
foreach ($xml->entry as $entry) {
    //Use Namespace
    $geo = $entry->children($namespaces['georss']); 
    echo $geo->point;
    echo "\n";
}
相关问题