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

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

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