SimpleXML:使用包含名称空间的XML [英] SimpleXML: Working with XML containing namespaces

查看:65
本文介绍了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>

我已经走了这么远,

$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");

在文档中,重点是我的:

From the docs, emphasis mine:

registerXPathNamespace […]为下一个XPath查询创建一个前缀/ns上下文 .

在此处可以找到更多处理SimpleXML中名称空间的方法,例如:
PHP的Stuart Herbert-使用SimpleXML解析RSS Feed

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆