将 XML SOAP 响应转换为 CSV

发布于 2025-01-07 08:16:09 字数 1364 浏览 1 评论 0原文

我需要以下方面的帮助:我编写了一个脚本来从 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 文件中没有输出,我的猜测是命名空间有问题,但我无法绕过它,即使我已经阅读了这里针对类似问题给出的所有解决方案。

任何帮助将不胜感激。

I need some help with the following: I wrote a script to get a response from a webservice and then to save it in an XML file on my server. This is the structure of the XML file:

<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>

I'm using this script to convert the XML file to 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);
}

The problem is that there is no output in the CSV file, my guess is that there is a problem with the namespaces, but I can't get around it, even though I've read all the solutions given here to similar problems.

Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2025-01-14 08:16:09

问题就在这里:

 $xml->Products->Product

看看你的xml结构,它是这样的:

<soap:Body>
  <GetCatalogResponse xmlns="http://tempuri.org/">
    <GetCatalogResult>
      <Products>
        <Product>

Products 位于某个节点内,你无法直接访问它。

problem is here:

 $xml->Products->Product

Look at your xml structure, it's like this:

<soap:Body>
  <GetCatalogResponse xmlns="http://tempuri.org/">
    <GetCatalogResult>
      <Products>
        <Product>

Products is inside some node, you can't access it directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文