[英]how to parse xml nodes with name attributes
I have an XML file that looks like: 我有一个XML文件,看起来像:
<sizes>
<size name="original" width="386" height="429">
http://userserve-ak.last.fm/serve/_/62380831/Adele+PNG.png
</size>
<size name="large" width="126" height="140">http://userserve-ak.last.fm/serve/126/62380831.png</size>
<size name="largesquare" width="126" height="126">
http://userserve-ak.last.fm/serve/126s/62380831.png
</size>
<size name="medium" width="64" height="71">http://userserve-ak.last.fm/serve/64/62380831.png</size>
<size name="small" width="34" height="38">http://userserve-ak.last.fm/serve/34/62380831.png</size>
<size name="extralarge" width="252" height="280">http://userserve-ak.last.fm/serve/252/62380831.png</size>
</sizes>
How would I get it to parse only the size node with the name "small"
? 如何获取仅解析名称为
"small"
的大小节点的信息?
So far I have the code: 到目前为止,我有代码:
echo $xml->images->image->sizes->size;
which works but it takes the first image, and I want to specifically locate the small image. 可以,但是需要第一张图像,我想专门定位小图像。
Will the small image always be the fifth one? 小图片会永远是第五张吗? If so, you could use
如果是这样,您可以使用
$xml->images->image->sizes->size[4];
$xml->images->image->sizes->size['name']
...should work good for getting any of them. $xml->images->image->sizes->size['name']
...应该可以很好地获取它们。 No matter how you do it, you'll want to watch out for the fact that the attribute is an object and in your situation you probably want it to be a string. 无论如何执行,都需要注意该属性是一个对象 ,在您的情况下,您可能希望它是一个字符串。 Which means you'll need to convert it to a string from an object, and you can do that with the
(string)
, like this: 这意味着您需要将其转换为来自对象的字符串,然后可以使用
(string)
,如下所示:
foreach ($xml->size as $size) {
$the_size = (string) size['name'];
print $the_size . "<br>";
}
That gets all of them & turns them into strings, so you can use string functions like strstr()
or strcmp()
. 这样就可以获取所有内容并将其转换为字符串,因此可以使用诸如
strstr()
或strcmp()
类的字符串函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.