How to get tag attributes using XML :: Simple?

I'm just trying to extract an attribute from XML into my Perl program. However, I am having problems retrieving attributes.

I am using XML :: Simple .

I can recover the information perfectly when the XML looks like this:

<IdList>
    <Id>17175540</Id>
</IdList>

using this code

 $data->{'DocSum'}->{'Id'};

However, when the XML looks like this:

<Item Name="Title" Type="String">
    Some Title
</Item>

I am not getting any data when using the following code

$data->{'DocSum'}->{'Title'};

By the way, this is the link I get from XML http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=19288470

+2
source share
5 answers

I took xml from the page you provided, used the whole thing as a string for the XMLin argument, and was successful with

print $data->{DocSum}->{Item}->[5]->{content};

- .

, .

Edit:

, , 6- - , , node, Name "Title" ( , , ):

foreach my $item_node (@{$data->{DocSum}->{Item}})
{
    if($item_node->{Name} eq 'Title')
    {
        print $item_node->{content};
        last;
    }
}

, Item DocSum, , PubType Title, - , PubTypeList node.

+3

:

$ perl -MXML::Simple -M'Data::Dump qw/pp/' 
my $ref = XMLin('<Item Name="Title" Type="String">Some Title</Item>');
pp $ref;

:

{ Name => "Title", Type => "String", content => "Some Title" }

, , "", .

+4

, XML:: Simple XML. , Data:: Dumper. .

use Data::Dumper;
print Dumper($data);
+2

, , "Title" , , , -. XPath, /DocSum/Item[@Name='Title']

XML:: Simple ( Perl),

my ( $item ) = grep { $_->{Name} eq 'Title' } @{$data->{DocSum}{Item}};

use List::Util qw<first>;
...
( first { $_->{Name} eq 'Title' } @{$data->{DocSum}{Item}} )->{content};

daotoad. , . , . , , DWIM.

+2

, XML:: Simple , . KeyAttr XMLin()?

+1

Source: https://habr.com/ru/post/1704991/


All Articles