将 XML 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 文件中没有输出,我的猜测是命名空间有问题,但我无法绕过它,即使我已经阅读了这里针对类似问题给出的所有解决方案。
任何帮助将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题就在这里:
看看你的xml结构,它是这样的:
Products
位于某个节点内,你无法直接访问它。problem is here:
Look at your xml structure, it's like this:
Products
is inside some node, you can't access it directly.