简单 XML 元素和来自 mobile.de 的 XML 数据的问题
Problem with SimpleXMLElement and XML data from mobile.de
我尝试从 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;
}
这是回显和 print_r 调用的输出:
0
Array ( [reference] => http://services.mobile.de/schema/reference )
我需要在 SimpleXML 对象上设置特定的命名空间吗??
我很感激任何答案,因为我真的被卡住了......
我需要 reference:item 的键值和 resource:local-描述值
我认为问题是 print_r 无法处理 xml 对象中的数据
试试这个:
echo $xml->asXML();
一个选项可能是使用 xpath 并在查询中使用命名空间并遍历结果:
/reference:reference/reference:item
并使用此查询为每个项目获取返回数组的第一个条目
./resource:local-description
注意对象的类型为SimpleXMLElement and you have to use __toString()或将它们转换为(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
我尝试从 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;
}
这是回显和 print_r 调用的输出:
0
Array ( [reference] => http://services.mobile.de/schema/reference )
我需要在 SimpleXML 对象上设置特定的命名空间吗??
我很感激任何答案,因为我真的被卡住了......
我需要 reference:item 的键值和 resource:local-描述值
我认为问题是 print_r 无法处理 xml 对象中的数据 试试这个:
echo $xml->asXML();
一个选项可能是使用 xpath 并在查询中使用命名空间并遍历结果:
/reference:reference/reference:item
并使用此查询为每个项目获取返回数组的第一个条目
./resource:local-description
注意对象的类型为SimpleXMLElement and you have to use __toString()或将它们转换为(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