XML parsing using perl -
i tried research on simple question have couldn't it. trying data web in xml , parse using perl. now, know how loop on repeating elements. but, stuck when not repeating (i know might silly). if elements repeating, put in array , data. but, when there single element throws , error saying 'not array reference'. want code such can parse @ both time (for single , multiple elements). code using follows:
use lwp::simple; use xml::simple; use data::dumper; open (fh, ">:utf8","xmlparsed1.txt"); $db1 = "pubmed"; $query = "13054692"; $q = 16354118; #for multiple mesh terms $xml = new xml::simple; $urlxml = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=$db1&id=$query&retmode=xml&rettype=abstract"; $dataxml = get($urlxml); $data = $xml->xmlin("$dataxml"); #print fh dumper($data); foreach $e(@{$data->{pubmedarticle}->{medlinecitation}->{meshheadinglist}->{meshheading}}) { print fh $e->{descriptorname}{content}, ' $$ '; }
also, can such separator $$ not printed after last element? tried following code:
$mesh = $data->{pubmedarticle}->{medlinecitation}->{meshheadinglist}->{meshheading}; while (my ($key, $value) = each(%$mesh)){ print fh "$value"; }
but, prints childnodes , want content node.
perl's xml::simple
take single item , return scalar, , if value repeats sends array reference. so, make code work, have force meshheading
return array reference:
$data = $xml->xmlin("$dataxml", forcearray => [qw( meshheading )]);
Comments
Post a Comment