Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Return values of tags which have the same name, in an array

by Anonymous Monk
on Aug 06, 2018 at 14:13 UTC ( [id://1219952]=note: print w/replies, xml ) Need Help??


in reply to Re: Return values of tags which have the same name, in an array
in thread Return values of tags which have the same name, in an array

Thanks for your quick reply, sorry for lack of information. I have added the full code below. I am trying to get the values of author into an array. Unfortuantely  my @array = map{$_->textContent} $authors->findnodes('./authors/author'); does not do this. Hopefuly this makes more sense

use CGI qw(-utf-8 :all *table); use LWP::Simple qw(get); use URI; use XML::LibXML; use Data::Dumper; use utf8; binmode(STDOUT, ":encoding(utf-8)"); my $parser = XML::LibXML->new(); $author = param('query'); $maxHits = param('maxHits'); my $url = URI->new("http://www.dblp.org/search/api/?"); my($q,$h,$c,$f,$format) = ($author,$maxHits,"4","0","xml"); $url->query_form( 'q' => $q, 'h' => $h, 'c' => $c, 'f' => $f, 'format' => $format, ); print header(-charset=>'utf-8'), "\n", start_html({-title=>'DBLP Search', -author=>'<>'}), "\n"; if (param('submit')) { my $xml= get($url); my $doc = $parser->parse_string( $xml); print h1("Authors"), "\n"; foreach my $authors ($doc->findnodes('/result/hits/hit/info')) { print $_->textContent, "\n" for $authors->findnodes('./authors +/author'); my @array = map{$_->textContent} $authors->findnodes('./author +s/author'); } } print h1("DBLP Query"), "\n"; print start_form({-method=>"POST" -action=>"http://cgi.csc.liv.ac.uk/cgi-bin/cgiwrap/u6ed/stat.pl"}) +; print label("query: "); print textfield({-name=>'query', -size=>200}), "\n"; print label("maxHits: "); print textfield({-name=>'maxHits', -size=>200}), "\n"; print br(), "\n"; print submit({-name=>'submit', -value=>'Submit'}), "\n"; print end_form, end_html

XML file:

<?xml version="1.0" encoding="UTF-8"?> <result> <query id="192448">Aderghal*</query> <status code="200">OK</status> <time unit="msecs">1.15</time> <completions total="1" computed="1" sent="1"> <c sc="5" dc="5" oc="5" id="19115731">aderghal</c> </completions> <hits total="5" computed="5" sent="5" first="0"> <hit score="1" id="80402"> <info><authors><author>Karim Aderghal</author><author>Alexander Khvost +ikov</author><author>Andrei Krylov</author><author>Jenny Benois-Pinea +u</author><author>Karim Afdel</author><author>Gwenaelle Catheline</au +thor></authors><title>Classification of Alzheimer Disease on Imaging +Modalities with Deep CNNs Using Cross-Modal Transfer Learning.</title +><venue>CBMS</venue><pages>345-350</pages><year>2018</year><type>Conf +erence and Workshop Papers</type><key>conf/cbms/AderghalKKBAC18</key> +<doi>10.1109/CBMS.2018.00067</doi><ee>https://doi.org/10.1109/CBMS.20 +18.00067</ee><url>https://dblp.org/rec/conf/cbms/AderghalKKBAC18</url +></info> <url>URL#80402</url> </hit> </hits> </result>

Replies are listed 'Best First'.
Re^3: Return values of tags which have the same name, in an array
by Eily (Monsignor) on Aug 06, 2018 at 14:33 UTC
    Unfortuantely my @array = map{$_->textContent} $authors->findnodes('./authors/author'); does not do this.

    What makes you say that? You don't use @array anywhere else in the code.

    You don't use strict or warnings in that code though, and @array is scoped to the for loop. This means that if you try to use it later, perl won't complain about the variable not existing, but the array from inside the loop will have already been deleted.

Re^3: Return values of tags which have the same name, in an array
by haukex (Archbishop) on Aug 06, 2018 at 17:42 UTC

    In addition to what Eily wrote, your XML parsing code works fine for me:

    As I said, perhaps you're looking for push?

    my @array; foreach my $authors ($doc->findnodes('/result/hits/hit/info')) { push @array, map {$_->textContent} $authors->findnodes('./authors/author'); } # use @array here

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1219952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found