#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0

    XML - CSV problem


    Hello I have the following xml results that are returned from a remote site

    Code:
    <ResultSet totalResultsAvailable="1">
      <Product orderNo="5321" partNo="A2345" truckable="1">
      <Manufacturer id="22">WIDGET 4 U</Manufacturer>
      <Model id="356">ACME 500</Model>
      <Years>95-98</Years>
      <ProductType id="23" categoryID="4">Cool Red Widgest</ProductType>
      <Material id="6">shiny stuff</Material>
      <PartNo>A2345</PartNo>
      <Code/>
    </Product>
    </ResultSet>
    I am simply trying to pull the xml results and place in a new csv file with the following code:

    but I get and error: Warning:

    Invalid argument supplied for foreach() in /home/myServer/public_html/xmlParser2.php on line 14

    Here is my code:

    PHP Code:
    <?
    echo 'Write XML to CSV';
    $basenameLong ='http://thisIsTheURLto.com/myFeed/?key=123456789&mode=getProducts;
    $fileNameCSV = '
    xmlParseContent.csv';

    $feedContent = '';    
    echo '
    <br/>Starting......';     
    $feedContent = file_get_contents($basenameLong);

    $fh = fopen($fileNameCSV, '
    w+'); //create new CSV file if not exists else append

    foreach($feedContent->ResultSet->Product as $product) {

        fputcsv($f, get_object_vars($product));
    }
    fclose($fh);


    ?>
    I know this code is very elementary but can you help me find the issue. I am a novice and I dont see it. I was using this thread to try to learn how to do this. http://stackoverflow.com/questions/9240393/php-xml-to-csv-conversion-issue
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,572
    Rep Power
    595
    As you can see from the formatter, your quotes are messed up. Why are you unquoting the csv filename?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    Originally Posted by gw1500se
    As you can see from the formatter, your quotes are messed up. Why are you unquoting the csv filename?
    Im defining the csv File name.... I thought...

    Code:
    $fileNameCSV = 'xmlParseContent.csv';
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,572
    Rep Power
    595
    Then you are missing the ending quote on the previous line. The formatter makes it pretty obvious.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    Fixed the typo and still same error.

    Thanks for your help.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    Here is my current code:

    Code:
    <?
    echo 'Write XML to CSV';
    $basenameLong ='http://thisIsTheURLto.com/myFeed/?key=123456789&mode=getProducts';
    $fileNameCSV = 'xmlParseContent.csv';
     
      	$feedContent = '';
    
    	echo '<br/>Starting......';
    	
    	$feedContent = file_get_contents($basenameLong);
     
    			$fh = fopen($fileNameCSV, 'w+'); //create new CSV file if not exists
    			
    			foreach($feedContent->ResultSet->Product as $product) {
     
    			fputcsv($fh, get_object_vars($product));
    			}
    	fclose($fh);
    
     
    ?>
    This is the line its complaining about:

    Code:
    foreach($feedContent->ResultSet->Product as $product) {
    Thank you very much
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,572
    Rep Power
    595
    That is what happens when you copy someone else's bad code without understanding what it does. 'get_file_contents' simply reads a file into a string. You are treating $feedContent as if it were an object instead. If you're thinking that is an XML object, that is where you went wrong. You need to parse that string into an XML object. See XML Parser.

    P.S. Why did you stop using [ PHP ] tags?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    Thank you for the information. I will go read up on XML Parser.

    THank you

    Sorry I dont undestand your question about php tag. My code is encased in php tags.

    Thanks again for you assistance.

IMN logo majestic logo threadwatch logo seochat tools logo