Page 1 of 1

Comma delimited multiple records txt file

Posted: Mon Oct 08, 2007 12:58 pm
by phpwalker
Hi guys,

So long I've not been here, hopefully someone here still remember who I am. :P

Alright, let's back to the topic. Previously I was creating a text to XML converter, which users can upload text files and convert them into XML files.

At first, each text file consists of a record. Now, I need to modify it for each text file consists of multiple records. Therefore, I will need to separate those records by using comma or any other suitable characters.

The First Method:
Previously, the only one record in the text file would look like this:

Code: Select all

name: phpwalker
age: 16
phone: 12345678
The Second Method:
Now, I want it to have more records in a text file, and convert all the records into separate XML files. My idea is to organize them like this:

Code: Select all

phpwalker, 16, 12345678; 
Alex, 17, 321654987;
phpfreak, 13, 22334460;
Mary, 15, 321654978;
My first attempt is for the first method is shown below:

Code: Select all

$file = $uploadfile;

$data = file($file) or die('Could not read file!');

foreach ($data as $line_num => $line) {

if (preg_match("/^\w+\W\s\w+/", $line)) {

list($field,$contents) = explode(":", $line);

$contents =htmlentities($contents);

$str .="<$field>$contents</$field>\n";

}
For the second method, I've some ideas now, I scan the first record at first row/line, then create the XML file for it. Then I move on to the next line and do the same things.

Or,

I scan through all the contents in the text file, find all the predefined delimiters and separate them into multiple records. Then only I loop through the records and convert them into XML.

I don't know which flow is the correct one, and don't know whether what I'm thinking is correct. Please help me...

Thanks in advance!

Posted: Mon Oct 08, 2007 7:18 pm
by feyd
fgetcsv() may be of interest.

Posted: Tue Oct 09, 2007 4:05 am
by phpwalker
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks feyd for the function!! It helps a lot.

Another thing bordering me is that I can have [b]a[/b] multi record text file, output to [b]a[/b] XML file as shown below.

Text input:
[quote]
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
[/quote]



XML output:
[syntax="xml"]
<document>
 <row>
  <Col0>John</Col0 >
  <Col1>Doe</Col1 >
  <Col2>120 jefferson st.</Col2 >
  <Col3>Riverside</Col3 >
  <Col4>NJ</Col4 >
  <Col5>08075</Col5 >
 </row>
 <row>
  <Col0>Jack</Col0 >
  <Col1>McGinnis</Col1 >
  <Col2>220 hobo Av.</Col2 >
  <Col3>Phila</Col3 >
  <Col4>PA</Col4 >
  <Col5>09119</Col5 >
 </row>
 <row>
  <Col0>John "Da Man"</Col0 >
  <Col1>Repici</Col1 >
  <Col2>120 Jefferson St.</Col2 >
  <Col3>Riverside</Col3 >
  <Col4>NJ</Col4 >
  <Col5>08075</Col5 >
 </row>
 <row>
  <Col0>Stephen</Col0 >
  <Col1>Tyler</Col1 >
  <Col2>7452 Terrace "At the Plaza" road</Col2 >
  <Col3>SomeTown</Col3 >
  <Col4>SD</Col4 >
  <Col5>91234</Col5 >
 </row>
 <row>
  <Col0></Col0 >
  <Col1>Blankman</Col1 >
  <Col2></Col2 >
  <Col3>SomeTown</Col3 >
  <Col4>SD</Col4 >
  <Col5>00298</Col5 >
 </row>
 <row>
  <Col0>Joan "the bone", Anne</Col0 >
  <Col1>Jet</Col1 >
  <Col2>9th, at Terrace plc</Col2 >
  <Col3>Desert City</Col3 >
  <Col4>CO</Col4 >
  <Col5>00123</Col5 >
 </row>
</document>
But, I need to output each row of records in a separate XML file. How do I achieve that? It's kinda hard to me.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Oct 09, 2007 9:32 am
by feyd
phpwalker wrote:But, I need to output each row of records in a separate XML file. How do I achieve that? It's kinda hard to me.
What do you not understand about the concept/implementation?

Posted: Tue Oct 09, 2007 1:55 pm
by phpwalker
Thanks for the XML syntax, I didn't realize that the existence of it. :o

Actually I understand the concept of how to implement it already, but I face the technical issue now.

I use the fgetcsv() to get the data.

test.txt
phpwalker, 16, street
fyed, 18, country
jonh, 15, garden
try.php

Code: Select all

$row = 1;
$handle = fopen("test.txt", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
        getfield($c);
    }
    
    createXML();

}
fclose($handle);


function getfield($no){

$names = array("name","age","address");

return $names[$no];

}

funtion createXML(){
// creatwe XML file, show their links
}
I dont know how to create the XML now... I need to store them into database, and show each of the XML links, i stucked here.[/syntax]

Posted: Tue Oct 09, 2007 5:02 pm
by Maugrim_The_Reaper
Have you looked at PHP5's DOM or SimpleXML functionality? Using one of these makes creating XML quite a simple task. The sole bother would be handling text files which may contain non-ASCII characters (needs conversion to UTF-8 with utf8_encode() or similar).

Both offer an OOP interface for adding new XML nodes, attributes, etc. It's a really useful bit of knowledge to have in PHP when so much interoperability formats these days are XML based.

Probably unrelated - but a format gaining traction in PHP is YAML. It's a text format which is quite like your first method. You can play around with by installing the PECL SYCK extension, or using a PHP library like Spyc.

Posted: Wed Oct 10, 2007 1:23 pm
by phpwalker
Thanks Maugrim for the hints. I have been using SimpleXML to get the values of the elements, but never realize that it has the function to add values in it.

Now I've to work hard on the coding part. If I've any problems again, sure will come back here as many pros are so PRO here. :idea:

Thanks again guys. :P

Posted: Wed Oct 10, 2007 11:21 pm
by phpwalker
Oh, my thread has moved to here.

Hi guys, don't now whether suitable to put it here or start a new topic.

But I choose to ask here first.

I've successfullllly created many XML file, but I've a problem now.

Previously, I use the datetime to auto generate the file name for the XML file.

Now I've generate more than a XML file at a time, cause users upload only one text file, but the script creates multiple XML files. What's an appropriate way to generate the file name for the XML file?

I've used milliseconds for the datetime but still all using the same file name. If using rand() I afraid there's a possibility to generate two same file name.

Any suggestion for me?

Posted: Thu Oct 11, 2007 8:32 am
by feyd
I suspect you only generate the filename once then.