开发者

Quick question about XML parsing

开发者 https://www.devze.com 2023-03-22 09:12 出处:网络
this might be a silly question but I am not getting it. I tried all ways, maybe I am making a silly mistake somewhere. I am still learning parsing. Your help will surely help me to enhance my knowledg

this might be a silly question but I am not getting it. I tried all ways, maybe I am making a silly mistake somewhere. I am still learning parsing. Your help will surely help me to enhance my knowledge. I want to extract forename and lastname of the authors from the authorlist. I have tried to write the code but not sure if I am right.

use LWP::Simple;
use XML::Simple;
use Data::Dumper;

open (FH, ">:utf8","xmlparsed1.txt");

my $db1 = "pubmed";
my $q = 16404398;
my $xml = new XML::Simple;
$urlxml = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=$db1&a开发者_如何转开发mp;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 the lastname but I want forename as well like 'Atul J Butte'. Also, as this is a generalized code for any such xml file is it correct to mention [0]? What if its at different position in some other xml file? Is there any other way to do this? Thank you.


You're forced into using the first array reference for AuthorList because you set ForceArray => ... AuthorList.

Instead try:

$data = $xml->XMLin("$dataxml", ForceArray => [qw( MeshHeading Author )]);
...
my $author_list = $data->{PubmedArticle}{MedlineCitation}{Article}{AuthorList}{Author};
foreach my $author ( @$author_list ) {
    print "Author: $author->{LastName}, $author->{ForeName}\n";
}
# Author: Butte, Atul J
# Author: Kohane, Isaac S

Note that $data->{foo}->{bar} is equivalent to $data->{foo}{bar}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号