XML Parsing with nested tags in perl

后端 未结 2 367
灰色年华
灰色年华 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:51

    You need to learn about references: perlreftut, perlref, perldsc.

    use strictures;
    use XML::Simple qw(:strict);
    
    my $root = XMLin(<<'XML', ForceArray => 0, KeyAttr => undef);
    
    .
    .
    at0004
    
    value
    + 
    
        DV_QUANTITY
        +
        
        +
        
        
            true
            false
            false
            false
            0.0
            1000.0
    
    mm[Hg]
    
    
    
    .
    .
    
    XML
    
    my $m = $root->{attributes}{children}{list}{magnitude};
    printf <<'TEMPLATE', $root->{node_id}, $m->{lower}, $m->{upper};
    node_id -> %s
        magnitude -> lower -> %.1f
        magnitude -> higher -> %.1f
    TEMPLATE
    
    use Data::Dump::Streamer qw(Dump); Dump $root;
    

    Output:

    node_id -> at0004
        magnitude -> lower -> 0.0
        magnitude -> higher -> 1000.0
    
    $HASH1 = {
        attributes => {
            children => {
                content => [("\n    +") x 2],
                list    => {
                    magnitude => {
                        lower           => '0.0',
                        lower_included  => 'true',
                        lower_unbounded => 'false',
                        upper           => '1000.0',
                        upper_included  => 'false',
                        upper_unbounded => 'false'
                    },
                    units => 'mm[Hg]'
                },
                node_id      => {},
                occurrences  => {},
                property     => {},
                rm_type_name => 'DV_QUANTITY',
                "xsi:type"   => 'C_DV_QUANTITY'
            },
            content           => "\n+",
            existence         => {},
            rm_attribute_name => 'value',
            "xsi:type"        => 'C_SINGLE_ATTRIBUTE'
        },
        content => [("\n.\n.\n") x 2],
        node_id => 'at0004'
    };
    

提交回复
热议问题