How can I use XML :: Simple with XML tags that can have one or more subitems?

I asked a question yesterday How to get tag attributes using XML :: Simple? which I use to get XML:

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=19273512 (1)

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=19291509 (2)

I made very good progress and wrote the following code that goes through the tags and searches for the ones I need. I am looking for a doi tag in the "ArticleIds" section

   foreach $item_node (@{$dataSummary->{DocSum}->{Item}})
        {
                if($item_node->{Name} eq 'ArticleIds')
                {
                        foreach $item_node1 (@{$item_node->{Item}})
                        {
                                if ($item_node1->{Name} eq 'doi')
                                {
                                    $doi=  $item_node1->{content};  
                                    last;
                                }
                        }
                        last;

                }
        }

This code basically looks for the ArticleIds tag and then looks for subtopics under it to find the doi tag.

, , , , ArticleIds ( (2)), . , ArticleIds ( (1)), .

Simple Parser . (1)

{ 'Type' => 'List', 'Item' => { 'Type' => 'String', 'content' => '19273512', 'Name' => 'pubmed' }, 'Name' => 'ArticleIds' }

(2)

{ 'Type' => 'List', 'Item' => [ { 'Type' => 'String', 'content' => '909564644', 'Name' => 'pii' }, { 'Type' => 'String', 'content' => '10.1080/13506120802676914', 'Name' => 'doi' }, { 'Type' => 'String', 'content' => '19291509', 'Name' => 'pubmed' } ], 'Name' => 'ArticleIds' }

. ArticleIds , , .

- ?

+3
3

Item, . Item, . , ForceArray. , , .

XMLin( 'file.xml', 
       ForceArray => qr{Item}x );

, , XML:: Simple . , ref ForceArray, . arrayref, :

XMLin( 'file.xml', 
       ForceArray => [ 'Item' ] );

XML:: Simple CPAN, , .

, XML:: Simple, , , ActiveState, , , . .

, ,

$item =~ /HASH/  # hash
$item =~ /ARRAY/ # array

ref ( )

ref($item) eq 'HASH' 
ref($item) eq 'ARRAY'
+6

, , , , - XML:: Simple, , , - .

- XML:: Twig. , XML , . , , .

, Twig, , XPath .., . XML, , XML:: Simple, .

+4

XML:: Simple. ref() .

+1

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


All Articles