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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I know, I can't figure out how to access that particular node. Any idea on how to fix the problem? Thanks!
first of all convert "<xml version="1.0" encoding="utf-8"?>" to "<xml version="1.0" encoding="utf-8"? /> second use "$xml->soap:Envelope->soap:Body->GetCatalogResponse->GetCatalogResult->Products->Product" instead of "$xml->Products->Product".
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
Any other suggestions? Please :)
Late info but, for others that come looking, here is a solution with the namespace stackoverflow.com/questions/3022256/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.