【问题标题】:SimpleXMLElement Access elements with namespace?SimpleXMLElement 使用命名空间访问元素?
【发布时间】:2012-10-03 16:04:44
【问题描述】:

我有以下 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);
}

【问题讨论】:

  • 我想将返回项解析为一个数组,就像 SimpleXMLElement 通常在不涉及命名空间时所做的那样。
  • 啊,好的,您收到任何错误消息吗?
  • 只是空对象:object(SimpleXMLElement)#83 (0) { }

标签: php xml simplexml xml-namespaces


【解决方案1】:

你只需要

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

【讨论】:

  • 人...肯定要在那里跳过一些箍。考虑到您只需要为非命名空间 XML 字符串执行 new SimpleXMLElement($xml) ,这似乎很荒谬。还有其他更好的 PHP XML 解析器吗?
  • 使用命名空间并不好玩,因为它们没有通用格式.....
  • 这里的复杂性来自于尝试将 XPath 与命名空间一起使用,并且不了解 SimpleXML 功能。我也不确定命名空间“没有通用格式”是什么意思 - AFAIK,每个命名空间都有一个全名(URI)和一个特定文档本地的任意别名。
【解决方案2】:

您需要使用-&gt;children() 方法。如果您避免使用 XPath 而只使用 SimpleXML 运算符,那么您的代码最终会简单得多。

命名空间元素也是使用标准 PHP 调试函数调试相当神奇的 SimpleXML 对象时不可见的许多事物之一。 (下面使用 [my simplexml_dump() function 代替。)

$data = new SimpleXMLElement($xml);
$banner = $data->children('ns1', true)->return; 
// Or to avoid relying on the 'ns1' alias: ->children('http://endpoint.website.com/', false);
simplexml_dump($banner);

请注意,在另一个 -&gt;children() 调用之前,“ns1”命名空间保持选中状态:

// Remember to cast to string if you want the content of an element or attribute
var_dump( (string)$banner->linkName );

如果&lt;return&gt;可以出现多次,这显然会变成:

foreach ( $data->children('ns1', true)->return as $banner )
{
    simplexml_dump($banner);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 2023-03-13
    • 2015-11-30
    • 2012-12-24
    • 2013-01-14
    • 1970-01-01
    相关资源
    最近更新 更多