Read from xml to 2D array

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::Dumper;
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;
 
P

Paul Lalli

Aravind said:
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??
^^^^^^
This comes so close to being a self-answered question, it's scary
http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm

Paul Lalli
 
P

Paul Lalli

Paul said:
Aravind said:
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??
^^^^^^
This comes so close to being a self-answered question, it's scary
http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm

For example:
$ perl -MXML::Simple -MData::Dumper -e'
my $in = XMLin(<<EOF);
<ALL>
<BOOK>
<TITLE>t1</TITLE>
<AUTHOR>a1</AUTHOR>
<ISBN>111</ISBN>
</BOOK>
<BOOK>
<TITLE>t2</TITLE>
<AUTHOR>a2</AUTHOR>
<ISBN>222</ISBN>
</BOOK>
</ALL>
EOF
my @a = map { [ @{$_}{qw/TITLE AUTHOR ISBN/} ] } @{$in->{BOOK}};

print Dumper($in, \@a);
'
$VAR1 = {
'BOOK' => [
{
'TITLE' => 't1',
'ISBN' => '111',
'AUTHOR' => 'a1'
},
{
'TITLE' => 't2',
'ISBN' => '222',
'AUTHOR' => 'a2'
}
]
};
$VAR2 = [
[
't1',
'a1',
'111'
],
[
't2',
'a2',
'222'
]
];


Paul Lalli
 
T

Ted Zlatanov

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??

Use XML::Simple. To get the data into the array, use something like
this. Note the enclosing <qqq> tags to make this a valid (enough for
parsing) XML document.

If you understand what $data contains (via the first Dumper() call),
the @array population process will make sense.

#!/usr/bin/perl

use warnings;
use strict;
use XML::Simple;
use Data::Dumper;

my $data = XMLin(\*DATA);
print Dumper $data;

# put the title, ISBN, and author into @array as an array reference,
# so @array is a two-dimensional list.

my @array = map { [ $_->{TITLE}, $_->{ISBN}, $_->{AUTHOR} ] } @{$data->{BOOK}};

print Dumper \@array;

__DATA__
<qqq>
<BOOK>
<TITLE>t1</TITLE>
<AUTHOR>a1</AUTHOR>
<ISBN>111</ISBN>
</BOOK>
<BOOK>
<TITLE>t2</TITLE>
<AUTHOR>a2</AUTHOR>
<ISBN>222</ISBN>
</BOOK>
</qqq>
 
A

Aravind

Thanks a lot Paul and Ted.
I didnt know about XML::Simple before. It was very helpful.

--Aravind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top