![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: XML question [Thread Next] Re: XML questionTo: NULL Date: 2/2/2007 8:03:00 PM On 2 Feb 2007 10:04:27 -0800, jencinas69@g... wrote: >The text file is tab delimited To parse a tab delimited file, you'll just need to read it in using fgetcsv, http://uk.php.net/fgetcsv - and for the delimiter value in the function call, specify the string "\t" (TAB character) Make sure you enclose that in double quotes not single quotes, for PHP to interpret it as a tab and not a the two literal characters \ and t Once you have that in an array, you can use foreach to create your nodes using DOMXML (PHP4) or DOM (PHP5), or even better for you, SimpleXML (PHP5) as it's a very lightweight processor. http://www.php.net/manual/en/ref.simplexml.php PS we don't know which version of PHP you're using, which is a very basic thing to know in order to propose a best solution. A typical example might look like this : test.csv : item1 item2 item3 item4 item5 item6 item7 item8 item9 item10 item11 item12 process.php : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <body> Loading... <br /> <?php // Column "headers" or names, assuming you don't know these already... $column_positions = array( 0 => 'column1', 1 => 'column2', 2 => 'column3', 3 => 'column4' ); $output = new SimpleXMLElement('<root />'); $file_resource = fopen('test.csv', 'r'); while ($linecontents = fgetcsv($file_resource, 0, "\t")) { // First, we want to create a new child node off the root... $row = $output->addChild('csv_row'); // Iterate each column of the resulting array from fgetcsv foreach($linecontents as $column => $data) { // To get "column headers" you could use a counter here and check for the // first iteration - and setup $column_positions as that array instead $item = $row->addChild($column_positions[$column], $data); } } ?> <textarea rows="20" cols=70"> <?php // Although we're printing this, you could save it to a file instead... print($output->asXML()); ?> </textarea> <br /> ... Finished </body> </html> Cheers - Neil ------------------------------------------------ Digital Media MVP : 2004-2007 http://mvp.support.microsoft.com/mvpfaqs | ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
