Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XML question

From: "Neil Smith [MVP Digital Media]" <neil@------.--->
To: 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


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent