Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by bleggee
at 2024-09-03 18:17:27
Point:500 Replies:8 POST_ID:829217USER_ID:12091
Topic:
PHP Scripting Language;;Extensible Markup Language (XML)
Does anyone have a PHP script that will read an XML file on a server located elsewhere, and write out the file in CSV format ?
Or know where I can find one (Bootcamp, HotScripts.com, etc) ?
Or know where I can find one (Bootcamp, HotScripts.com, etc) ?
Expert: Ray Paseur replied at 2024-09-15 05:40:53
This seems to work mostly OK. Put in your server name and credentials (lines 8-12) and give it a try. I say mostly because I'm getting a blank line at the end of the CSV file. Not sure why that happens.
<?php // demo/temp_bleggee.phperror_reporting(E_ALL);// For FTP access see http://php.net/manual/en/book.ftp.php// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28511220.html// REF: http://www.php.net/manual/en/function.simplexml-load-string.php// WHERE IS THE FTP DATA?$ftp_server = "???.net";$ftp_user_name = "???";$ftp_user_pass = "???";$ftp_file_name = '???.xml';// WHERE IS THE OUTPUT FILE PATH?$xml = 'storage/bleggee.xml';// WHERE DOES THE CSV GO?$csv = 'storage/bleggee.csv';// REF: http://www.php.net/manual/en/function.ftp-connect.php$ftp_stream = ftp_connect($ftp_server);if (!$ftp_stream) trigger_error("UNABLE TO CONNECT TO $ftp_server", E_USER_ERROR);// REF: http://www.php.net/manual/en/function.ftp-login.php$login_result = ftp_login($ftp_stream, $ftp_user_name, $ftp_user_pass);if (!$login_result) trigger_error("UNABLE TO LOGIN TO $ftp_server USING $ftp_user_name + $ftp_user_pass", E_USER_ERROR);// REF: http://www.php.net/manual/en/function.ftp-get.phpftp_pasv($ftp_stream, TRUE);// COPY THE FILE$xfer_result = ftp_get($ftp_stream, $xml, $ftp_file_name, FTP_BINARY);if (!$xfer_result) trigger_error("UNABLE TO GET $ftp_file_name", E_USER_ERROR);ftp_close($ftp_stream);// OPEN THE CSV FILE$fpw = fopen($csv, 'w');if (!$fpw) trigger_error("UNABLE TO OPEN $csv FOR WRITE", E_USER_ERROR);// GET AN OBJECT FROM THE XML$obj = SimpleXML_Load_File($xml);// GET AN ARRAY WITH THE NAMES OF THE PROPERTIES OF THE OBJECT$arr = array_flip( (array)$obj->{'EXPORT-RECORDS'}->PRODUCT[0] );fputcsv($fpw, $arr);// USE AN ITERATOR TO ACCESS THE DATA IN THE PROPERTIES OF THE OBJECTforeach ($obj->{'EXPORT-RECORDS'}->PRODUCT as $dat){ // CONVERT OBJECT TO ARRAY FOR FPUTCSV() $dat = (array)$dat; fputcsv($fpw, $dat);}// ALL DONEfclose($fpw);// SHOW A LINK TO THE FILEecho PHP_EOL. 'See: '. '<a target="_blank" href="'. $csv. '">'. $csv. '</a>'; 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:
Author: bleggee replied at 2024-09-10 19:10:11
Thx Ray, you're absolutely right, I'll post the debug questions separately.
As for the FTP input, I am taking your info & trying to code it up myself. Best way to learn of course :)
I'll post back here if I get stuck.
- Brian
As for the FTP input, I am taking your info & trying to code it up myself. Best way to learn of course :)
I'll post back here if I get stuck.
- Brian
Expert: Ray Paseur replied at 2024-09-10 07:18:24
Well, those are very different questions but I'll try to help. You may want to post follow-up questions here at E-E.
1. PHP has FTP support.
http://www.php.net/manual/en/book.ftp.php
2. Komodo and Eclipse are two popular Integrated Development Environments ("IDE"). PHPUnit is one of the popular tools used for testing.
http://komodoide.com/
http://www.eclipse.org/pdt/
https://phpunit.de/
Understanding the problem well enough to write your own test cases may be all you need (sometimes). Example here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
1. PHP has FTP support.
http://www.php.net/manual/en/book.ftp.php
2. Komodo and Eclipse are two popular Integrated Development Environments ("IDE"). PHPUnit is one of the popular tools used for testing.
http://komodoide.com/
http://www.eclipse.org/pdt/
https://phpunit.de/
Understanding the problem well enough to write your own test cases may be all you need (sometimes). Example here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
Author: bleggee replied at 2024-09-09 20:31:39
Great Ray ! I ran your script on my server as well, worked flawlessly! A few questions:
#1 The "real" XML file exists on a Server where I have ftp server name, login, & password (Not http access). I presume that I have to change line 9 to something else?
#2 What is used these days for debugging PHP scripts, other than adding lines to put out messages like "You've arrived at Line 9" and displaying variable contents ? Like running the script step-by-step with an immediate window? I use Coda2 to do my PHP code hacking but no idea what debugging tools are decent these days.
#1 The "real" XML file exists on a Server where I have ftp server name, login, & password (Not http access). I presume that I have to change line 9 to something else?
#2 What is used these days for debugging PHP scripts, other than adding lines to put out messages like "You've arrived at Line 9" and displaying variable contents ? Like running the script step-by-step with an immediate window? I use Coda2 to do my PHP code hacking but no idea what debugging tools are decent these days.
Expert: Ray Paseur replied at 2024-09-09 02:10:58
<?php // demp/temp_blegee.phperror_reporting(E_ALL);// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28511220.html// REF: http://www.php.net/manual/en/function.simplexml-load-string.php// REF: http://www.php.net/manual/en/function.fputcsv.php// WHERE IS THE TEST DATA?$url = 'http://filedb.experts-exchange.com/incoming/2014/09_w37/871127/products-ee.xml';$xml = file_get_contents($url);$obj = SimpleXML_Load_String($xml);// WHERE IS THE OUTPUT FILE PATH?$csv = 'storage/bleggee.csv';$fpw = fopen($csv, 'w');if (!$fpw) trigger_error("UNABLE TO OPEN $csv", E_USER_ERROR);// GET AN ARRAY WITH THE NAMES OF THE PROPERTIES OF THE OBJECT$arr = array_flip( (array)$obj->{'EXPORT-RECORDS'}->PRODUCT[0] );fputcsv($fpw, $arr);// USE AN ITERATOR TO ACCESS THE DATA IN THE PROPERTIES OF THE OBJECTforeach ($obj->{'EXPORT-RECORDS'}->PRODUCT as $dat){ // CONVERT OBJECT TO ARRAY FOR FPUTCSV() $dat = (array)$dat; fputcsv($fpw, $dat);}// ALL DONEfclose($fpw);// SHOW A LINK TO THE FILEecho PHP_EOL. 'See: '. '<a target="_blank" href="'. $csv. '">'. $csv. '</a>'; 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:
Author: bleggee replied at 2024-09-08 17:57:23
Hi Ray - Here's a small 4-item copy of the actual XML file (attached).
A tested & working PHP example would be awesome !!
The vendor gives me FTP Access, then there's this XML file in a folder on the vendor's remote server.
A tested & working PHP example would be awesome !!
The vendor gives me FTP Access, then there's this XML file in a folder on the vendor's remote server.
Expert: Ray Paseur replied at 2024-09-04 06:21:15
Please post a link to the XML file in question.
XML : CSV may or may not be a 1 : 1 correspondent relationship, so we would need to see the XML document and know exactly what part(s) of it you want rendered into the CSV strings. Once we know that part, it's only a few lines of code and we can probably give you a tested and working example.
XML : CSV may or may not be a 1 : 1 correspondent relationship, so we would need to see the XML document and know exactly what part(s) of it you want rendered into the CSV strings. Once we know that part, it's only a few lines of code and we can probably give you a tested and working example.
Expert: rwniceing replied at 2024-09-03 18:49:41
You can use php curl describes at php.net site at http://php.net/manual/en/book.curl.php to get xml file at remote server and follow instructions for using fputcsv() and simplexml_load_file() convert xml to csv file which
describes at this link http://twiwoo.com/php/php-xml-to-csv/
describes at this link http://twiwoo.com/php/php-xml-to-csv/