首页 > 解决方案 > 带有嵌套图像标签的 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"
    }
    ...
  }
}

标签: phpxmlsimplexml

解决方案


你没有为你的 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

3v4l.org 上的演示


推荐阅读