PHP 返回嵌套 XML

发布于 2024-10-10 14:54:28 字数 6762 浏览 1 评论 0原文

我正在按照下面的代码从文件中检索 XML。然而,当 XML 在 ".$image["url"]." 处变得更深时,我试图检索其中一张图像,但它不起作用。我还需要搜索图像,因为 XML 中的 SIZE 变量有多个标签(下面的 XML)。

在我已经将 $xml->xpath 定义到更高的容器之后,您如何定义它?

$searchurl = "movies.xml";
$xml = simplexml_load_file($searchurl);
$i = 0;
foreach($xml->xpath('/movies/movie') as $movie){
    $image = $xml->xpath('/movies/movie/images/image');
    echo "<table id=\"ctable\">"
        ."<thead><tr><th>"
        ."<th width=\"100%\"><a href=\"JavaScript:void(0);\" onclick=\"showHide('movie".$movie->id."','span".$movie->id."')\">"
        ."<span id=\"span".$movie->id."\"><img src=\"images\icon_collapse.gif\" class=\"expand\" alt=\"Expand\"></span>"
        ."<span>".$movie->name."</span></a></th>"
        ."<th><span></span></th>"
        ."</th></tr></thead>";
    echo "<table><tfoot id=\"movie".$movie->id."\">"
        ."<tr><td rowspan=\"6\"><a class=\"thumbnail\" href=\"#thumb\"><img src=\"".$image["url"]."\" height=\"150px\" width=\"150px\"><span><img src=\"".$image["url"]."\"/><br/>".$movie->name."</span></a></td><td rowspan=\"6\"><img class=\"line\" width=\"2\" height=\"100%\"></td><td class=\"jralign\">Movie ID:</td><td>".$movie->id."</td></tr>"
        ."<tr><td class=\"jralign\">Movie Link:</td><td><a href=\"".$movie->url."\">".$movie->url."</a></td></tr>"
        ."<tr><td class=\"jralign\">Released:</td><td>".$movie->released."</td></tr>"
        ."<tr><td class=\"jralign\">Classification:</td><td>".$movie->certification."</td></tr>"
        ."<tr><td class=\"jralign\">Adult:</td><td>".$movie->adult."</td></tr>"
        ."<tr><td class=\"jralign\" valign=\"top\">Overview:</td><td>".$movie->overview."</td></tr>"
        ."</tfoot></table>"
        ."</table><br/>";
    $i++;
}

XML 它正在搜索。

<?xml version="1.0" encoding="UTF-8"?> 
<movies> 
  <movie> 
    <score></score> 
    <popularity>3</popularity> 
    <translated></translated> 
    <adult>false</adult> 
    <language>en</language> 
    <original_name>Transformers</original_name> 
    <name>Transformers</name> 
    <alternative_name>The Transformers</alternative_name> 
    <type>movie</type> 
    <id>1858</id> 
    <imdb_id>tt0418279</imdb_id> 
    <url>http://www.themoviedb.org/movie/1858</url> 
    <votes>61</votes> 
    <rating>7.4</rating> 
    <certification>PG-13</certification> 
    <overview>Young teenager Sam Witwicky becomes involved in the ancient struggle between two extraterrestrial factions of transforming robots, the heroic Autobots and the evil Decepticons.  Sam holds the clue to unimaginable power and the Decepticons will stop at nothing to retrieve it.</overview> 
    <released>2007-07-04</released> 
    <images> 
      <image type="poster" url="transformers-original.jpg" size="original" width="1000" height="1500" id="4c9ea5cf5e73d67049000233"/> 
      <image type="poster" url="transformers-mid.jpg" size="mid" width="500" height="750" id="4c9ea5cf5e73d67049000233"/> 
      <image type="poster" url="transformers-cover.jpg" size="cover" width="185" height="278" id="4c9ea5cf5e73d67049000233"/> 
      <image type="poster" url="transformers-thumb.jpg" size="thumb" width="92" height="138" id="4c9ea5cf5e73d67049000233"/> 
      </images> 
      <version>141</version> 
      <last_modified_at>2011-01-04 16:33:25</last_modified_at> 
    </movie>
</movies>

更新功能:不显示图像。

        $searchurl = "movies.xml";
        $xml = simplexml_load_file($searchurl); 
foreach($xml->xpath('/movies/movie') as $movie){
            $images = $movie->xpath('/images/image');
            $image_url = '';
            foreach($images as $image) {
                $attributes = $image->attributes();
                if ($attributes['size'] == 'thumb') {
                    $image_url = $attributes['url'];
                    break;
                }
            }
            echo "<table id=\"ctable\">"
                ."<thead><tr><th>"
                ."<th width=\"100%\"><a href=\"JavaScript:void(0);\" onclick=\"showHide('movie".$movie->id."','span".$movie->id."')\">"
                ."<span id=\"span".$movie->id."\"><img src=\"images\icon_collapse.gif\" class=\"expand\" alt=\"Expand\"></span>"
                ."<span>".$movie->name."</span></a></th>"
                ."<th><span>".$this->isWatching($movie->id, $uid, $movie->name, 'movie')."</span></th>"
                ."</th></tr></thead>";
            echo "<table><tfoot id=\"movie".$movie->id."\">"
                ."<tr><td rowspan=\"6\"><a class=\"thumbnail\" href=\"#thumb\"><img src=\"".$image_url."\" height=\"150px\" width=\"150px\"><span><img src=\"".$image_url."\"/><br/>".$movie->name."</span></a></td><td rowspan=\"6\"><img class=\"line\" width=\"2\" height=\"100%\"></td><td class=\"jralign\">Movie ID:</td><td>".$movie->id."</td></tr>"
                ."<tr><td class=\"jralign\">Movie Link:</td><td><a href=\"".$movie->url."\">".$movie->url."</a></td></tr>"
                ."<tr><td class=\"jralign\">Released:</td><td>".$movie->released."</td></tr>"
                ."<tr><td class=\"jralign\">Classification:</td><td>".$movie->certification."</td></tr>"
                ."<tr><td class=\"jralign\">Adult:</td><td>".$movie->adult."</td></tr>"
                ."<tr><td class=\"jralign\" valign=\"top\">Overview:</td><td>".$movie->overview."</td></tr>"
                ."</tfoot></table>"
                ."</table><br/>";
            $i++;
        }

i am retrieving XML from a file as per code below. However when the XML gets deeper at the point of ".$image["url"]." i am trying to retrieve one of the images but it is not working. I would also need to search the images as there are multiple tags by the SIZE variable in the XML (XML below).

how do you define this after i have allready defined the $xml->xpath to a higher container?

$searchurl = "movies.xml";
$xml = simplexml_load_file($searchurl);
$i = 0;
foreach($xml->xpath('/movies/movie') as $movie){
    $image = $xml->xpath('/movies/movie/images/image');
    echo "<table id=\"ctable\">"
        ."<thead><tr><th>"
        ."<th width=\"100%\"><a href=\"JavaScript:void(0);\" onclick=\"showHide('movie".$movie->id."','span".$movie->id."')\">"
        ."<span id=\"span".$movie->id."\"><img src=\"images\icon_collapse.gif\" class=\"expand\" alt=\"Expand\"></span>"
        ."<span>".$movie->name."</span></a></th>"
        ."<th><span></span></th>"
        ."</th></tr></thead>";
    echo "<table><tfoot id=\"movie".$movie->id."\">"
        ."<tr><td rowspan=\"6\"><a class=\"thumbnail\" href=\"#thumb\"><img src=\"".$image["url"]."\" height=\"150px\" width=\"150px\"><span><img src=\"".$image["url"]."\"/><br/>".$movie->name."</span></a></td><td rowspan=\"6\"><img class=\"line\" width=\"2\" height=\"100%\"></td><td class=\"jralign\">Movie ID:</td><td>".$movie->id."</td></tr>"
        ."<tr><td class=\"jralign\">Movie Link:</td><td><a href=\"".$movie->url."\">".$movie->url."</a></td></tr>"
        ."<tr><td class=\"jralign\">Released:</td><td>".$movie->released."</td></tr>"
        ."<tr><td class=\"jralign\">Classification:</td><td>".$movie->certification."</td></tr>"
        ."<tr><td class=\"jralign\">Adult:</td><td>".$movie->adult."</td></tr>"
        ."<tr><td class=\"jralign\" valign=\"top\">Overview:</td><td>".$movie->overview."</td></tr>"
        ."</tfoot></table>"
        ."</table><br/>";
    $i++;
}

XML It is searching through.

<?xml version="1.0" encoding="UTF-8"?> 
<movies> 
  <movie> 
    <score></score> 
    <popularity>3</popularity> 
    <translated></translated> 
    <adult>false</adult> 
    <language>en</language> 
    <original_name>Transformers</original_name> 
    <name>Transformers</name> 
    <alternative_name>The Transformers</alternative_name> 
    <type>movie</type> 
    <id>1858</id> 
    <imdb_id>tt0418279</imdb_id> 
    <url>http://www.themoviedb.org/movie/1858</url> 
    <votes>61</votes> 
    <rating>7.4</rating> 
    <certification>PG-13</certification> 
    <overview>Young teenager Sam Witwicky becomes involved in the ancient struggle between two extraterrestrial factions of transforming robots, the heroic Autobots and the evil Decepticons.  Sam holds the clue to unimaginable power and the Decepticons will stop at nothing to retrieve it.</overview> 
    <released>2007-07-04</released> 
    <images> 
      <image type="poster" url="transformers-original.jpg" size="original" width="1000" height="1500" id="4c9ea5cf5e73d67049000233"/> 
      <image type="poster" url="transformers-mid.jpg" size="mid" width="500" height="750" id="4c9ea5cf5e73d67049000233"/> 
      <image type="poster" url="transformers-cover.jpg" size="cover" width="185" height="278" id="4c9ea5cf5e73d67049000233"/> 
      <image type="poster" url="transformers-thumb.jpg" size="thumb" width="92" height="138" id="4c9ea5cf5e73d67049000233"/> 
      </images> 
      <version>141</version> 
      <last_modified_at>2011-01-04 16:33:25</last_modified_at> 
    </movie>
</movies>

Updated Function: Not displaying images.

        $searchurl = "movies.xml";
        $xml = simplexml_load_file($searchurl); 
foreach($xml->xpath('/movies/movie') as $movie){
            $images = $movie->xpath('/images/image');
            $image_url = '';
            foreach($images as $image) {
                $attributes = $image->attributes();
                if ($attributes['size'] == 'thumb') {
                    $image_url = $attributes['url'];
                    break;
                }
            }
            echo "<table id=\"ctable\">"
                ."<thead><tr><th>"
                ."<th width=\"100%\"><a href=\"JavaScript:void(0);\" onclick=\"showHide('movie".$movie->id."','span".$movie->id."')\">"
                ."<span id=\"span".$movie->id."\"><img src=\"images\icon_collapse.gif\" class=\"expand\" alt=\"Expand\"></span>"
                ."<span>".$movie->name."</span></a></th>"
                ."<th><span>".$this->isWatching($movie->id, $uid, $movie->name, 'movie')."</span></th>"
                ."</th></tr></thead>";
            echo "<table><tfoot id=\"movie".$movie->id."\">"
                ."<tr><td rowspan=\"6\"><a class=\"thumbnail\" href=\"#thumb\"><img src=\"".$image_url."\" height=\"150px\" width=\"150px\"><span><img src=\"".$image_url."\"/><br/>".$movie->name."</span></a></td><td rowspan=\"6\"><img class=\"line\" width=\"2\" height=\"100%\"></td><td class=\"jralign\">Movie ID:</td><td>".$movie->id."</td></tr>"
                ."<tr><td class=\"jralign\">Movie Link:</td><td><a href=\"".$movie->url."\">".$movie->url."</a></td></tr>"
                ."<tr><td class=\"jralign\">Released:</td><td>".$movie->released."</td></tr>"
                ."<tr><td class=\"jralign\">Classification:</td><td>".$movie->certification."</td></tr>"
                ."<tr><td class=\"jralign\">Adult:</td><td>".$movie->adult."</td></tr>"
                ."<tr><td class=\"jralign\" valign=\"top\">Overview:</td><td>".$movie->overview."</td></tr>"
                ."</tfoot></table>"
                ."</table><br/>";
            $i++;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2024-10-17 14:54:28
$images = $xml->xpath('/movies/movie/images/image');
$needed_image_url = '';
foreach($images as $image) {
    $attributes = $image->attributes();
    if ($attributes['size'] == 'thumb') {
        $needed_image_url = $attributes['url'];
        break;
    }
}

例如,这将获取缩略图图像 url。

$images = $xml->xpath('/movies/movie/images/image');
$needed_image_url = '';
foreach($images as $image) {
    $attributes = $image->attributes();
    if ($attributes['size'] == 'thumb') {
        $needed_image_url = $attributes['url'];
        break;
    }
}

this would grab the thumb image url for instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文