XML Parsing with nested tags in perl

后端 未结 2 378
灰色年华
灰色年华 2021-01-24 02:53

I am trying to parse the xml file which has collection of nested tags.I was trying with perl XML::Simple API to parse and individual tag values are parsed exactly but couldn ab

2条回答
  •  悲哀的现实
    2021-01-24 03:26

    Here's an XML::Twig program that can do it, although I've made some assumptions that you might have to adjust. I don't know if can have more than one node-attributes pairs, so I wrote this to handle multiple pairs:

    #!/Users/brian/bin/perls/perl5.14.2
    
    use XML::Twig;
    use Data::Dumper;
    
    my $twig = XML::Twig->new(
        twig_handlers => {
            magnitude => sub {
                my $m = $_;
                my $hash = $m->simplify;
                my $node_id = $m->parent( 'attributes' )->prev_sibling( 'node_id' )->text;
                print "node -> $node_id\n",
                    "\tmagnitude -> lower -> $hash->{lower} $units\n",
                    "\tmagnitude -> higher -> $hash->{upper} $units\n";
                },
            },
        );
    
    $twig->parse(*DATA);
    
    
    __END__
    
    
    at0004
    
        value
         
        
            DV_QUANTITY
            
            
            
            
                
                    true
                    false
                    false
                    false
                    0.0
                    1000.0
                
                mm[Hg]
            
        
    
    
    at0005
    
        value
         
        
            DV_QUANTITY
            
            
            
            
                
                    true
                    false
                    false
                    false
                    100.9
                    998.7
                
                mm[Hg]
            
        
    
    
    
    

提交回复
热议问题