A
Aravind
I have an xml file that has tags such as say:
<BOOK>
<TITLE>t1</TITLE>
<AUTHOR>a1</AUTHOR>
<ISBN>111</ISBN>
</BOOK>
<BOOK>
<TITLE>t2</TITLE>
<AUTHOR>a2</AUTHOR>
<ISBN>222</ISBN>
</BOOK>
....
I want to read the data into a 2D array eg: array[0][0] = t1,
array[0][1]=a1, ...
What is the simplest way to achieve this??
I was thinking of this (which works) but if xml file has a lot of sub
tags then its cumbersome:
#!/usr/local/bin/perl -w
use diagnostics;
use Data:umper;
open FIN,'test.txt' or die "Cannot open file. $!";
$index = 0;
while(<FIN>)
{
chomp;
print "\n Now: '$_'";
if(m{<TITLE>(.+)</TITLE>}) {$array[$index][0] = $1;}
if(m{<AUTHOR>(.+)</AUTHOR>}) {$array[$index][1] = $1;}
if(m{<ISBN>(.+)</ISBN>}) {$array[$index][2] = $1;}
if(m{</BOOK>}) {$index++;}
}
print Dumper(\@array);
close FIN;
<BOOK>
<TITLE>t1</TITLE>
<AUTHOR>a1</AUTHOR>
<ISBN>111</ISBN>
</BOOK>
<BOOK>
<TITLE>t2</TITLE>
<AUTHOR>a2</AUTHOR>
<ISBN>222</ISBN>
</BOOK>
....
I want to read the data into a 2D array eg: array[0][0] = t1,
array[0][1]=a1, ...
What is the simplest way to achieve this??
I was thinking of this (which works) but if xml file has a lot of sub
tags then its cumbersome:
#!/usr/local/bin/perl -w
use diagnostics;
use Data:umper;
open FIN,'test.txt' or die "Cannot open file. $!";
$index = 0;
while(<FIN>)
{
chomp;
print "\n Now: '$_'";
if(m{<TITLE>(.+)</TITLE>}) {$array[$index][0] = $1;}
if(m{<AUTHOR>(.+)</AUTHOR>}) {$array[$index][1] = $1;}
if(m{<ISBN>(.+)</ISBN>}) {$array[$index][2] = $1;}
if(m{</BOOK>}) {$index++;}
}
print Dumper(\@array);
close FIN;