php - 来自 mobile.de 的 SimpleXMLElement 和 XML 数据的问题
问题描述
我尝试从 mobile.de 获取 xml 数据来准备我的插件数据。返回的 XML 如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reference:reference xmlns:seller="http://services.mobile.de/schema /seller"
xmlns:ad="http://services.mobile.de/schema/ad"
xmlns:financing="http://services.mobile.de/schema/common/financing-1.0"
xmlns:reference="http://services.mobile.de/schema/reference"
xmlns:resource="http://services.mobile.de/schema/resource">
<reference:item key="Cabrio" url="https://services.mobile.de/refdata/classes/Car/categories/Cabrio">
<resource:local-description xml-lang="en">Cabriolet / Roadster</resource:local-description>
</reference:item>
<reference:item key="EstateCar" url="https://services.mobile.de/refdata/classes/Car/categories/EstateCar">
<resource:local-description xml-lang="en">Estate Car</resource:local-description>
</reference:item>
<reference:item key="Limousine" url="https://services.mobile.de/refdata/classes/Car/categories/Limousine">
<resource:local-description xml-lang="en">Saloon</resource:local-description>
</reference:item>
<reference:item key="OffRoad" url="https://services.mobile.de/refdata/classes/Car/categories/OffRoad">
<resource:local-description xml-lang="en">SUV/Off-road Vehicle/Pickup Truck</resource:local-description>
</reference:item>
<reference:item key="SmallCar" url="https://services.mobile.de/refdata/classes/Car/categories/SmallCar">
<resource:local-description xml-lang="en">Small Car</resource:local-description>
</reference:item>
<reference:item key="SportsCar" url="https://services.mobile.de/refdata/classes/Car/categories/SportsCar">
<resource:local-description xml-lang="en">Sports Car/Coupe</resource:local-description>
</reference:item>
<reference:item key="Van" url="https://services.mobile.de/refdata/classes/Car/categories/Van">
<resource:local-description xml-lang="en">Van/Minibus</resource:local-description>
</reference:item>
<reference:item key="OtherCar" url="https://services.mobile.de/refdata/classes/Car/categories/OtherCar">
<resource:local-description xml-lang="en">Other</resource:local-description>
</reference:item>
</reference:reference>
我试图遍历节点,但似乎 SimpleXML 无法解析这个 xml。
这是我的php代码:
public function getReferenceXML($url) {
//url e.g : https://services.mobile.de/refdata/classes/Car/categories
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = htmlspecialchars_decode(curl_exec($ch));
curl_close($ch);
$file = EAZY_CLASSIC_CARS_ROOT_DIR . basename($url) . '.xml';
if (file_exists($file)) {
unlink($file);
}
file_put_contents($file, $data);
$xml = simplexml_load_file($file);
echo $xml->count();
echo '</br>';
print_r($xml->getNamespaces());
die();
//return $xml;
}
这是 echo 和 print_r 调用的输出:
0
Array ( [reference] => http://services.mobile.de/schema/reference )
我需要在 SimpleXML 对象上设置一个特定的命名空间吗?
我很感激任何答案,因为我真的被卡住了......
我需要参考的关键值:项目和资源:本地描述值
解决方案
一种选择可能是使用 xpath 并在查询中使用命名空间并遍历结果:
/reference:reference/reference:item
对于每个项目,使用此查询获取返回数组的第一个条目
./resource:local-description
请注意,对象是SimpleXMLElement类型,您必须使用__toString()或将它们强制转换为 a(string)
以获取字符串内容
例如:
foreach($xml->xpath("/reference:reference/reference:item") as $item) {
$key = (string)$item->attributes()["key"];
$description = (string)$item->xpath("./resource:local-description")[0];
echo "Key: " . $key . PHP_EOL;
echo "Description " . $description . PHP_EOL;
echo PHP_EOL;
}
输出
Key: Cabrio
Description Cabriolet / Roadster
Key: EstateCar
Description Estate Car
Key: Limousine
Description Saloon
Key: OffRoad
Description SUV/Off-road Vehicle/Pickup Truck
Key: SmallCar
Description Small Car
Key: SportsCar
Description Sports Car/Coupe
Key: Van
Description Van/Minibus
Key: OtherCar
Description Other
推荐阅读
- c# - System.InvalidCastException:对象不能存储在这种类型的数组中。在 C# 中
- python - python3 和 psycopg2,检查 dict_values 和 dict_keys
- python - 无法将文件从 DBFS 复制到 Databricks 中的本地桌面
- python - 使用 groupby 命令从数据框列表中堆叠条形图
- pandas - 生成总和为 1 的值列表 - 在 groupby 内?
- sql - Oracle 存储过程未按预期加载数据
- python - Python pandas根据日期和列添加小时修改数据框
- regex - 写入数据时公式不会停止跳行
- ruby-on-rails - vscode扩展颜色设置会覆盖settings.json颜色设置,如何解决?
- node.js - 从 React js 应用程序读取快速会话 cookie