php - 将 XML SOAP 响应转换为 CSV

标签 php xml web-services soap csv

我需要以下方面的帮助:我编写了一个脚本来从 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/

相关文章:

php - 构建更好的搜索查询 php/mysql

php - 找不到类错误,命名空间使用错误吗?

android - RecyclerView 滚动时隐藏工具栏会导致文本移至通知栏

xml - 使用经典 ASP 编写完整的 XML 文件

php - 用mysql数据替换函数中的数组

php - 如何在CodeIgniter中为403禁止的错误设置自定义页面

regex - 查找距离方差为零且记录瓦数的条目

java - 实现 Java REST Web 服务的最简单框架

java - 使用 Spring 代理时如何查看和调试 Web 服务响应?

java - 服务架构- webservies | SOAP