如果另一个属性具有特定值,则仅获取属性
好的,关于 stackoverflow 的第一个问题。
我有以下 xml:
<movies>
<movie>
<cast>
<person name="Tim Johnson" character="" job="Director"/>
<person name="Avril Lavigne" character="Heather (voice)" job="Actor"/>
<person name="Omid Djalili" character="Tiger (voice)" job="Actor"/>
<person name="Karey Kirkpatrick" character="" job="Director"/>
</cast>
</movie>
</movies>
我像这样检索它:
<?php $xml_getinfo_result = new SimpleXMLElement(file_get_contents($tmdb_getinfo_result)); ?>
要获得演员阵容,我使用以下内容:
$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
$tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
echo "<li>".$tmdb_actors."</li>";
$i++;
}
这给了我:
<li>Tim Johnson</li>
<li>Avril Lavigne</li>
<li>Omid Djalili</li>
<li>Karey Kirkpatrick</li>
但是,如果我只想显示工作为“演员”的人员,我需要做什么?
Ok, first question on stackoverflow.
I have the following xml:
<movies>
<movie>
<cast>
<person name="Tim Johnson" character="" job="Director"/>
<person name="Avril Lavigne" character="Heather (voice)" job="Actor"/>
<person name="Omid Djalili" character="Tiger (voice)" job="Actor"/>
<person name="Karey Kirkpatrick" character="" job="Director"/>
</cast>
</movie>
</movies>
I retrieve it like this:
<?php $xml_getinfo_result = new SimpleXMLElement(file_get_contents($tmdb_getinfo_result)); ?>
To get the cast, I use the following:
$i = 0;
while ($xml_getinfo_result->movies->movie->cast->person[$i]) {
$tmdb_actors = $xml_getinfo_result->movies->movie->cast->person[$i]->attributes()->name;
echo "<li>".$tmdb_actors."</li>";
$i++;
}
This gives me:
<li>Tim Johnson</li>
<li>Avril Lavigne</li>
<li>Omid Djalili</li>
<li>Karey Kirkpatrick</li>
But what do I need to do if I want to display only the persons whose job is "Actor"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以这样做:
you can do this:
两种可能性:
第一种,获取所有数据并仅显示演员:
第二种是仅解析作为演员的
并显示所有结果。Two possibilities:
The first one, you get all data and you display only actor:
The second one is to parse only
<person>
that are actor and display all result.您可以使用 xpath:
You could use xpath:
我会把它放在 foreach 语句中
I'd throw it in a foreach statement