perl - Quick question about XML parsing -
this might silly question not getting it. tried ways, maybe making silly mistake somewhere. still learning parsing. surely me enhance knowledge. want extract forename , lastname of authors authorlist. have tried write code not sure if right.
use lwp::simple; use xml::simple; use data::dumper; open (fh, ">:utf8","xmlparsed1.txt"); $db1 = "pubmed"; $q = 16404398; $xml = new xml::simple; $urlxml = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=$db1&id=$q&retmode=xml&rettype=abstract"; $dataxml = get($urlxml); $data = $xml->xmlin("$dataxml", forcearray => [qw( meshheading authorlist )]); print fh dumper($data); print fh "authors: ".join '$$', map $_->{lastname},@{$data->{pubmedarticle}->{medlinecitation}->{article}->{authorlist}->[0]->{author}};
this gives me lastname want forename 'atul j butte'. also, generalized code such xml file correct mention [0]? if @ different position in other xml file? there other way this? thank you.
you're forced using first array reference authorlist because set forcearray => ... authorlist
.
instead try:
$data = $xml->xmlin("$dataxml", forcearray => [qw( meshheading author )]); ... $author_list = $data->{pubmedarticle}{medlinecitation}{article}{authorlist}{author}; foreach $author ( @$author_list ) { print "author: $author->{lastname}, $author->{forename}\n"; } # author: butte, atul j # author: kohane, isaac s
note $data->{foo}->{bar} equivalent $data->{foo}{bar}
Comments
Post a Comment