English 中文(简体)
根据属性价值确定XML要素
原标题:Getting XML element based on attribute value

Hey. I have an XML file and would like to use PHP to display the medium size only. The part of the XML looks like this:

<image size="small">/small23.png</image>
<image size="medium">/medium23.png</image>
<image size="large">/large23.png</image>

我用<代码><img src=”/>在网页上展示图像,但我不敢肯定如何只展示中的形象。 在通过所有其他内容的论坛内,我尝试:

if($file->image->attributes()->size == "medium")
   echo "<img src=$file->image />";

但没有。

感谢

最佳回答

根据您的榜样,我假定你重新使用简单的XML,这样就可以:

$mediums = $file->xpath("image[@size= medium ]");
if (count($mediums)) {
    echo  <img src="  . (string) $mediums[0] .  " /> ;
}
问题回答


$done = false;
foreach($file->image as $img) {
    foreach($img->attributes() as $key => $value) {
        if($key == "size" && $value == "medium") {
            echo "<img src = {$file->image} />";
            $done = true;
            break;
        }
    }
    if($done) { break; }
}
This might work, try it out and let me know.




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签