php - 如何在 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);
}
有人能帮我吗?
解决方案
推荐阅读
- tkinter - 使用 tkinter 滑块输入来计算 Black Scholes 期权价格,使用 lambda 不起作用
- r - 如何根据 R 中的 ID 删除数据集的最后 N 行?
- python - 为多个 CSV 文件导出 Pandas 输出
- scala - 找不到参数映射器的隐式值
- javascript - Jest mock setTimeout - 出现超时错误
- haskell - 为什么 Haskell 不接受这种列表语法?
- c++ - 并行创建特征行主要稀疏矩阵
- node.js - Firestore 数组包含不适用于云 Firebase 管理员
- c++ - Arduino USB 串行通信抛出错误,“avrdude: ser_open(): can't set com-state for "\\.\COM3"
- angular - 如何使可编辑字段类型在 ag 网格角度中获取日期?