2

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.

alexius
  • 73
  • 2
  • 9

1 Answers1

2

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.

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
Duke
  • 1,731
  • 2
  • 17
  • 33
  • I know, I can't figure out how to access that particular node. Any idea on how to fix the problem? Thanks! – alexius Feb 21 '12 at 16:43
  • 1
    first of all convert "" to " second use "$xml->soap:Envelope->soap:Body->GetCatalogResponse->GetCatalogResult->Products->Product" instead of "$xml->Products->Product". – Duke Feb 21 '12 at 16:47
  • I've already tried this option, but I get a PHP error: PHP Parse error: syntax error, unexpected ':' in line 9 which is $xml->soap:Envelope->soap:Body->GetCatalogResponse->GetCatalogResult->Products->Product – alexius Feb 21 '12 at 16:56
  • Any other suggestions? Please :) – alexius Feb 21 '12 at 17:28
  • 1
    Late info but, for others that come looking, here is a solution with the namespace http://stackoverflow.com/questions/3022256/simplexml-soap-response-namespace-issues – winner_joiner Nov 26 '15 at 17:11