CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[HELP] Using a Perl scrip to read a Text File and parse data into XML

 



SteffenBaier
Novice

Jul 13, 2009, 7:48 AM

Post #1 of 5 (810 views)
[HELP] Using a Perl scrip to read a Text File and parse data into XML Can't Post

Hi,



I am trying to use PERL to read a text file and convert the result into a XML file.

the source just contains 2 entrys seperated by a comma.

Example:

Code
doe,joe,1001 
Parker,Jane,1002
Smith,bill,1003



The output should look like this:

Code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<!-- $RCSfile: 000000000000-directory~.xml,v $ $Revision: 1.3 $ -->
<directory>
<item_list>
<item>
<ln>Doe</ln>
<fn>John</fn>
<ct>1001</ct>
<sd>1</sd>
<rt>3</rt>
<dc/>
<ad>0</ad>
<ar>0</ar>
<bw>0</bw>
<bb>0</bb>
</item>
<item>
<ln>Parker</ln>
<fn>Jane</fn>
<ct>1002</ct>
<sd>2</sd>
<rt>3</rt>
<dc/>
<ad>0</ad>
<ar>0</ar>
<bw>0</bw>
<bb>0</bb>
</item>
<item>
<ln>Smith</ln>
<fn>Bill</fn>
<ct>1003</ct>
<sd>3</sd>
<rt>3</rt>
<dc/>
<ad>0</ad>
<ar>0</ar>
<bw>0</bw>
<bb>0</bb>
</item>
</item_list>
</directory>



My PERL Script so far is able to display part of the Data as I would expect it but the written File only contains the last entry on the original source...

Perl Script used:


Code
#!/usr/bin/perl   

#print "<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n"; #This actually does not work but I have not checked why it cannot be printed
print "<directory>\n";
print " <item_list>\n";
print " <item>\n";

open (FILE, '<C:/Temp/test.txt');
while (<FILE>) {
chomp;
($lastname, $firstname, $phone) = split("\,");
print " <ln>$lastname</ln>\n";
print " <ln>$firstname</ln>\n";
print " <ct>$phone</ct>\n";
print " </item>\n";
}
print " </item_list>\n";
print " </directory>\n";

open CTI, ">C:/temp/directory-000000000000.xml";
print CTI<<eof ;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<directory>
<item_list>
<item>
<ln>$lastname</ln>
<fn>$firstname</fn>
<ct>$phone</ct>
</item>
</item_list>
</directory>;

eof
close CTI;
close (FILE);
exit;



KevinR
Veteran


Jul 13, 2009, 10:23 AM

Post #2 of 5 (808 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse data into XML [In reply to] Can't Post

The only challenge here is this data:

<sd>1</sd>
<rt>3</rt>

'sd' seems to be the position the name is found at and 'tr' seems to be the total number of entries in the file.

Does the file continue on like your sample data in order:

1001
1002
1003
1004
1005
.
.
.
1???

does that ever change and are the above assumptions correct?
-------------------------------------------------


(This post was edited by KevinR on Jul 13, 2009, 10:24 AM)


SteffenBaier
Novice

Jul 13, 2009, 11:15 AM

Post #3 of 5 (800 views)
Re: [KevinR] [HELP] Using a Perl scrip to read a Text File and parse data into XML [In reply to] Can't Post

Hi Ya,

the only relevant Data (never tried to get the phone to load the file without it...)




Code
<sd>1</sd>



This is the position of the Speed Dial in the order the phone shows it on free buttons. I already increased that so that is covered ;-)

Any help would be great


(This post was edited by SteffenBaier on Jul 13, 2009, 11:26 AM)


ichi
User

Jul 13, 2009, 10:11 PM

Post #4 of 5 (791 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse data into XML [In reply to] Can't Post


Code
$S = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 
$S = $S . "<!-- \$RCSfile: 000000000000-directory~.xml,v \$ \$Revision: 1.3 \$ -->\n";
$S = $S . "<directory>\n";
$S = $S . "<item_list> \n";

while (<>) {
chomp;
($f1,$f2) = split(/,/);
$S = $S . "<item>\n";
$S = $S . ' <ln>' . $f1 . "</ln>\n";
$S = $S . ' <fn>' . $f2 . "</fn>\n";
$S = $S . " <ct>1001</ct>\n";
$S = $S . ' <sd>' . ++$d . "</sd>\n";
$S = $S . " <rt>3</rt>\n";
$S = $S . " <dc/>\n";
$S = $S . " <ad>0</ad>\n";
$S = $S . " <ar>0</ar>\n";
$S = $S . " <bw>0</bw>\n";
$S = $S . " <bb>0</bb>\n";
$S = $S . "</item>\n";
}
$S = $S . " </item_list>\n";
$S = $S . ' </directory>';
print $S;


you could also use a here document to write you XML.
On the command line

Code
# perl myscript.pl > new.xml



SteffenBaier
Novice

Jul 14, 2009, 1:23 AM

Post #5 of 5 (787 views)
Re: [ichi] [HELP] Using a Perl scrip to read a Text File and parse data into XML [In reply to] Can't Post

Hi Ichi,

apologize but I should have made myself a bit clearer.

The txt File I read contains: Last Name, First Name and then a Phone Number. This can be external or internal.

My Script with Part of your code above now creates a XML File with the Header etc. but only includes the last Row of data contained in the text file.
I included the "normal" Print Command so I can see in the Command Line that the Perl Script actually get's the Data but somehow the XML File that is written only includes the last Entry.

My latest Version of the Perl Script:

Code
#!/usr/bin/perl   

$i=0;
print "<directory>\n";
print " <item_list>\n";
print " <item>\n";

open (FILE, '<C:/Temp/test.txt');
while (<FILE>) {
chomp;
($lastname, $firstname, $phone) = split("\,");
$i++;
print " <ln>$lastname</ln>\n";
print " <ln>$firstname</ln>\n";
print " <ct>$phone</ct>\n";
print " <sd>$i</sd>\n";
print " </item>\n";
}
print " </item_list>\n";
print "</directory>\n";

open CTI, ">C:/temp/directory-000000000000.xml";
print CTI<<eof ;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<directory>
<item_list>
<item>
<ln>$lastname</ln>
<fn>$firstname</fn>
<ct>$phone</ct>
<sd>$i</sd>
</item>
</item_list>
</directory>;

eof
close CTI;
close (FILE);
exit;



The Screen shows (I have not actually included all the Headers when using PRINT i):


Code
<directory> 
<item_list>
<item>
<ln>doe</ln>
<ln>joe</ln>
<ct>1001 </ct>
<sd>1</sd>
</item>
<ln>Parker</ln>
<ln>Jane</ln>
<ct>1002 </ct>
<sd>2</sd>
</item>
<ln>Smith</ln>
<ln>bill</ln>
<ct>1003</ct>
<sd>3</sd>
</item>
</item_list>
</directory>

Code
  



The Created XML File contains:


Code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<!-- $RCSfile: 000000000000-directory~.xml,v $ $Revision: 1.3 $ -->
<directory>
<item_list>
</item>
<ln>Smith</ln>
<fn>bill</fn>
<ct>1003</ct>
<sd>3</sd>
</item>
</item_list>
</directory>





So close but yet so far ;-)

THX

Steff

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives