SimpleXML: Working with XML containing namespaces(SimpleXML:使用包含命名空间的 XML)
问题描述
我正在尝试通过 google-picasa API 获取地理信息.这是原始 XML:
I am trying to get to the geo information off the google-picasa API. This is the original XML:
<georss:where>
<gml:Point>
<gml:pos>35.669998 139.770004</gml:pos>
</gml:Point>
</georss:where>
我已经走到这一步了,有:
I already have come this far, with:
$ns_geo=$item->children($namespace['georss']);
$geo=$ns_geo->children($namespace['gml']);
var_dump($geo) 会输出
object(SimpleXMLElement)#34 (1) {
["Point"]=> object(SimpleXMLElement)#30 (1) {
["pos"]=> string(18) "52.373801 4.890935"
}
}
但是
echo (string)$geo->position or (string)$geo->position->pos;
不会给我任何东西.有什么明显的我做错了吗?
will give me nothing. Is there something obvious that i am doing wrong?
推荐答案
您可以使用 XPath 和 registerXPathNamespace():
You could work with XPath and registerXPathNamespace():
$xml->registerXPathNamespace("georss", "http://www.georss.org/georss");
$xml->registerXPathNamespace("gml", "http://www.opengis.net/gml");
$pos = $xml->xpath("/georss:where/gml:Point/gml:pos");
从文档中,强调我的:
registerXPathNamespace […] 为下一个 XPath 查询创建一个前缀/ns 上下文.
registerXPathNamespace […] Creates a prefix/ns context for the next XPath query.
更多在 SimpleXML 中处理命名空间的方法可以在这里找到,例如:
Stuart Herbert 在 PHP - 使用 SimpleXML解析 RSS 源
More ways to handle namespaces in SimpleXML can be found here, for example:
Stuart Herbert On PHP - Using SimpleXML To Parse RSS Feeds
这篇关于SimpleXML:使用包含命名空间的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SimpleXML:使用包含命名空间的 XML
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
