simpleXML 根据属性获取节点子节点 [英] simpleXML get node child based on attribute

查看:29
本文介绍了simpleXML 根据属性获取节点子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析我通过其属性之一引用的节点的值.但我不确定语法

I am trying to parse out the value of a node I am referencing by one of its attributes. but I am not sure of the syntax

XML:

<data>

  <poster name="E-Verify" id="everify">

    <full_image url="e-verify-swa-poster.jpg"/>

    <full_other url=""/>

  </poster>


  <poster name="Minimum Wage" id="minwage">

    <full_image url="minwage.jpg"/>

    <full_other url="spa_minwage.jpg"/>

  </poster>
</data>

这里是我想要获取 full_image 的 url 值的地方,其中海报 id 等于 minwage:

here is where I want to get the url value of full_image where poster id equal to minwage:

$xml = simplexml_load_file('PosterData.xml');
$main_url = $xml->full_image[name] where poster[id] = "minwage";
//something like that.
echo $main_url;

Result: minwage.jpg

如果有人有任何涵盖这些内容的资源,请分享.

If anyone has any resources that cover this stuff please share.

推荐答案

简单地循环海报元素并记住将属性值转换为字符串,因为您想比较它们(并且可能将它们输出)作为字符串:

Simply loop the poster elements and remember to cast the attribute values to strings, since you want to compare them (and probably output them) as strings:

$xml = simplexml_load_file('PosterData.xml');

foreach ($xml->poster as $poster) {
    if ((string) $poster['id'] == 'minwage') {
        echo (string) $poster->full_image['url'];
    }
}

这篇关于simpleXML 根据属性获取节点子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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