首页 > 解决方案 > 如何在 php 中访问 xml 文件的子元素,在这种情况下,有多个条目带有不同分辨率的 url

问题描述

我有一个 xml 文件的以下部分:

<ad:ad xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:resource="http://services.mobile.de/schema/resource" xmlns:ad="http://services.mobile.de/schema/ad" url="https://services.mobile.de/search-api/ad/266399529" xsi:schemaLocation="http://services.mobile.de/schema/ad http://services.mobile.de/schema/ad-1.0.xsd">
  <ad:images>
    <ad:image>
      <ad:representation size="S" url="$_18.JPG"/>
      <ad:representation size="XL" url="$_27.JPG"/>
      <ad:representation size="ICON" url="$_23.JPG"/>
      <ad:representation size="L" url="$_1.JPG"/>
      <ad:representation size="M" url="$_24.JPG"/>
    </ad:image>

我现在想访问大小为“L”的表示元素并在那里读取 URL。但我就是无法访问这些子元素。

这是我的代码到目前为止的样子

            $ad_ns = $ad_xml->children('http://services.mobile.de/schema/ad');
            foreach ( $ad_ns->images->image as $image )    {
              $image_attributes = $image->representation[0]->attributes();
              $image_url = (string) $image_attributes['gallery-url'];
              $image_url_big = str_replace("23.JPG", "27.JPG", $image_url);
              $image_url_small = str_replace("22.JPG", "1.JPG", $image_url);
            }

有人能帮我吗?

标签: phpxml

解决方案


推荐阅读