php - 带有嵌套图像标签的 XML 上的 SimpleXML_Load_String 未捕获“url”属性
问题描述
我试图在运行 PHP 5.3 的旧服务器上使用 simplexml_load_string 解析 xml。xml 中的标签之一是并且有几个具有 url 属性的嵌套子级。如果我对 SimpleXMLElement 进行 var_dump,则图像只是一个字符串数组(每个标签的文本节点子节点),没有 url 属性的符号。
关于为什么标签上的 url 属性没有被捕获的任何想法?
我尝试将图像标签上的 url 属性缩短到大约 15 个字符,但它们仍然没有被捕获。
我尝试将属性作为图像元素的数组元素以及通过调用图像元素上的 attributes() 方法来访问。
示例 XML
<properties xmlns:xs="http://www.w3.org/2001/XMLSchema">
<property>
<reference>####</reference>
...
<images>
<image url="https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_JUxtL~PunupG3r34QY7hcc7wMtgouw9c1H6DUbVNANjxM_Zg--.jpg">A2970_10776141.jpg</image>
<image url="https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_AwVzHs_Ajc3bPh6tAooCEbZkq_ZYbRT5eAjUXsq3Znh_f~Vw--.jpg">A2970_10776142.jpg</image>
...
<links/>
</property>
<property>
...
</property>
</properties>
我的代码
$feed = file_get_contents('test.xml');
$properties = simplexml_load_string($feed);
foreach ($properties as $property) {
var_dump($property->images);
exit;
}
结果
object(SimpleXMLElement)#5 (1) {
["image"]=>
array(19) {
[0]=>
string(18) "A2970_10776141.jpg"
[1]=>
string(18) "A2970_10776142.jpg"
...
}
}
我也试过:
foreach ($property->images as $image)
{
var_dump($image['url']);
var_dump($image->attributes());
}
$image['url'] 输出 NULL
$image->attributes() 输出关于在非对象上调用 attributes() 的错误
理想情况下, var_dumping $property->images 会产生:
object(SimpleXMLElement)#5 (1) {
["image"]=>
array(19) {
[0]=>
object(SimpleXMLElement)#6 (1) {
["@attributes"]=>
array(1) {
["url"]=>
string(162) "https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_JUxtL~PunupG3r34QY7hcc7wMtgouw9c1H6DUbVNANjxM_Zg--.jpg"
}
}
[text]=> "A2970_10776141.jpg"
}
...
}
}
解决方案
你没有为你的 foreach 循环使用正确的值,你实际上应该使用$property->images->image
它,因为它是你想要迭代的那些元素。之后,您可以选择访问url
属性的方法:
$properties = simplexml_load_string($feed);
foreach ($properties as $property) {
foreach ($property->images->image as $image) {
echo $image['url'] . PHP_EOL;
echo $image->attributes()['url'] . PHP_EOL;
}
}
输出:
https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_JUxtL~PunupG3r34QY7hcc7wMtgouw9c1H6DUbVNANjxM_Zg--.jpg
https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_JUxtL~PunupG3r34QY7hcc7wMtgouw9c1H6DUbVNANjxM_Zg--.jpg
https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_AwVzHs_Ajc3bPh6tAooCEbZkq_ZYbRT5eAjUXsq3Znh_f~Vw--.jpg
https://#####.#########.###/imgV1/QGcM5YXB7ITu20usXVas9zwwpfszaQl0S7VJWTlCweLw8h1OJgGrQ8SrQZiKyUwAW5EgBoJga_AwVzHs_Ajc3bPh6tAooCEbZkq_ZYbRT5eAjUXsq3Znh_f~Vw--.jpg
推荐阅读
- spring-boot - 在 Flux/Mono 反应管道之外抛出异常
- sas - 在 SAS 中合并 Proc GLM 的输出文件
- javascript - JavaScript 意外错误消息(未捕获的 SyntaxError:预期表达式,得到 '}')
- flutter - Tailwindcss 颜色托盘在颤动中的模拟器上不显示相同的颜色
- azure-devops - 使用 Power Shell 脚本下载 DacPac
- python - SQLAlchemy ORM 多个多对多关系
- github - 在 vscode 中打开项目时,git 扩展从我的笔记本电脑中抓取文件
- html - 移动浏览器 - 网站中的哪个元元素是这个图标
- arrays - Spark:GroupBy和collect_list同时按另一列过滤
- python - 为什么 grpc.FutureTimeoutError 不是 grpc.RpcError 和 grpc.Call 的实例?