On 19 mai, 11:12, dr.virus.in...@gmail.com (Chaitanya Yanamadala)
wrote:
> hai i require one more help
> i have an xml like this
>
> [ snip XML ]
>
> now what i wanted is that i need to fetch the values of tag email and show
> them.
> how do i do it..
>
> use XML::Simple;
>
> my $conf=XMLin('x.xml',forcearray=>1);
> use Data::Dumper;
> print Dumper($conf);
> print $conf->{timestamp}."\n";
> @email=$conf->{customer}->{email};

@email = map {$_->{email}[0]} @{$conf->{customer}};

Here is the problem, you need to acknowledge that XML::Simple has
created an array in $conf->{customer}, and for each element in this
array you want to reference the e-mail (and because XML::Simple
presents the e-mails also in an array, you want the first, i.e. index
[0] ).

> foreach(@email)
> {
>         print "$_\n";
> }

But if you find XML::Simple confusing (at least I find it confusing),
you can always look for alternatives, for example in the document "XML
- Ways to Rome" on http://xmltwig.com/article/ways_to_rome/ways_to_rome.html

In that document you find modules like XML::Parser, XML::Twig,
XML::PYX, XML::TokeParser, XML::LibXML, etc... You might or might not
like each of those modules, but it is good to know what's out there.

If you find that none of the modules described in "XML - Ways to Rome"
fit your requirements, than you should think again of what you want to
achieve, how big is your data set, does it fit entirely into memory,
etc... then have a look again at the document "XML - Ways to Rome" and
make up your mind.

If you * really, really, really * reject all modules in the document
"XML - Ways to Rome", then you could have a look at my blatant plug
"XML::Reader" http://search.cpan.org/~keichner/XML-Reader-0.35/lib/XML/Reader.pm

Here is an example program that uses XML::Reader to solve your
problem:

use strict;
use warnings;

use XML::Reader;

my $rdr = XML::Reader->new('x.xml', {mode => 'branches'},
  { root => '/spam-document/customer',
   branch => ['/first-name', '/surname', '/email'] });

while ($rdr->iterate) {
    my ($first, $last, $email) = $rdr->rval;
    printf "%-20s, %-20s, %s\n", $first, $last, $email;
}


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to