php - SimpleXMLElement 使用命名空间访问元素?

标签 php xml simplexml xml-namespaces

我有以下 XML:

<ns1:getBannerLinksResponse xmlns:ns1="http://endpoint.website.com/">
    <ns1:return>
        <ns1:campaignID>0</ns1:campaignID>
        <ns1:categoryID>200230455</ns1:categoryID>
        <ns1:categoryName>Promotion</ns1:categoryName>
        <ns1:linkID>10001599</ns1:linkID>
        <ns1:linkName>KFL-20% off No Min</ns1:linkName>
        <ns1:mid>3071</ns1:mid>
        <ns1:nid>1</ns1:nid>
        <ns1:clickURL>
            http://someurl
        </ns1:clickURL>
        <ns1:endDate>Oct 15, 2012</ns1:endDate>
        <ns1:height>250</ns1:height>
        <ns1:iconURL>
            http://someurl
        </ns1:iconURL>
        <ns1:imgURL>
            http://someurl
        </ns1:imgURL>
        <ns1:landURL>
            http://someurl
        </ns1:landURL>
        <ns1:serverType>22</ns1:serverType>
        <ns1:showURL>
            http://someurl
        </ns1:showURL>
        <ns1:size>13</ns1:size>
        <ns1:startDate>Oct 14, 2012</ns1:startDate>
        <ns1:width>300</ns1:width>
    </ns1:return>
</ns1:getBannerLinksResponse>

我尝试了以下但没有成功:

$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns1','http://endpoint.website.com/');
foreach($data->xpath('//ns1:return') as $banner)
{
  $banner->registerXPathNamespace('ns1','http://endpoint.website.com/');
  var_dump($banner);
}

最佳答案

你只需要

$data = new SimpleXMLElement($xml);
$data->registerXPathNamespace('ns1','http://endpoint.websitecom/');
$part = $data->xpath("//ns1:return");
var_dump($part[0]->children("ns1",true));

输出

object(SimpleXMLElement)[3]
  public 'campaignID' => string '0' (length=1)
  public 'categoryID' => string '200230455' (length=9)
  public 'categoryName' => string 'Promotion' (length=9)
  public 'linkID' => string '10001599' (length=8)
  public 'linkName' => string 'KFL-20% off No Min' (length=18)
  public 'mid' => string '3071' (length=4)
  public 'nid' => string '1' (length=1)
  public 'clickURL' => string '
            http://someurl
        ' (length=36)
  public 'endDate' => string 'Oct 15, 2012' (length=12)
  public 'height' => string '250' (length=3)
  public 'iconURL' => string '
            http://someurl
        ' (length=36)
  public 'imgURL' => string '
            http://someurl
        ' (length=36)
  public 'landURL' => string '
            http://someurl
        ' (length=36)
  public 'serverType' => string '22' (length=2)
  public 'showURL' => string '
            http://someurl
        ' (length=36)
  public 'size' => string '13' (length=2)
  public 'startDate' => string 'Oct 14, 2012' (length=12)
  public 'width' => string '300' (length=3)

关于php - SimpleXMLElement 使用命名空间访问元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865858/

相关文章:

php - 在 Laravel 中关联()

c# - 如何重定向 .nuget 文件夹? nuget包与SDK nuget包的区别

android:popupBackground 不适用于上下文菜单

php - 递归地合并两个 XML 文件

php - 使用 SimpleXML 编写 XML 文件时如何使用命名空间

php - 比较目录状态或散列以获得乐趣和利润的最快方法

javascript - 无法使用 PHP 和 JavaScript 验证表单输入字段

php - 用 PHP 编写的自定义 CMS,我应该向客户端发送 HTTP 内容长度 header 吗?

java - 使用 Transformer 缩进 XML 内部数据

php - 在 PHP 中使用 'simplexml_load_file()' 读取 RSS 源时仅显示项目符号列表