php – SimpleXMLElement访问带命名空间的元素?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – SimpleXMLElement访问带命名空间的元素?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2168字,纯文字阅读大概需要4分钟。
内容图文
![php – SimpleXMLElement访问带命名空间的元素?](/upload/InfoBanner/zyjiaocheng/707/ffdcfafd2b4e488b8883c0c447013828.jpg)
我有以下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访问带命名空间的元素?全部内容,希望文章能够帮你解决php – SimpleXMLElement访问带命名空间的元素?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。