我需要以下方面的帮助:我编写了一个脚本来从 Web 服务获取响应,然后将其保存在服务器上的 XML 文件中。这是 XML 文件的结构:
<xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCatalogResponse xmlns="http://tempuri.org/">
<GetCatalogResult>
<Products>
<Product>
<Code>1234</Code>
<Name>product name</Name>
<Category>some category</Category>
<Manufacturer>manufacturer</Manufacturer>
<Price>100</Price>
<Stock>1</Stock>
</Product>
</Products>
</GetCatalogResult>
</GetCatalogResponse>
</soap:Body>
</soap:Envelope>
我正在使用此脚本将 XML 文件转换为 CSV:
$filexml='filename.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('filename.csv', 'w');
// I only need the CSV to contain two columns, the product code and the corresponding price
foreach($xml->Products->Product as $Product) {
$values = array("Code" => $Product->Code, "Price" => $Product->Price);
fputcsv($f, $values,',','"');
}
fclose($f);
}
问题是 CSV 文件中没有输出,我猜测命名空间有问题,但我无法解决它,即使我已经阅读了此处给出的所有类似解决方案问题。
任何帮助将不胜感激。
最佳答案
问题出在这里:
$xml->Products->Product
看看你的xml结构,是这样的:
<soap:Body>
<GetCatalogResponse xmlns="http://tempuri.org/">
<GetCatalogResult>
<Products>
<Product>
Products
位于某个节点内,您无法直接访问它。
关于php - 将 XML SOAP 响应转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9381285/