hi all

this is the file contents with set of tags (not xml file)
<CCCMAP> <COMPONENT> <COMP_NAME>ip</COMP_NAME> <CCC> <CCC_NAME>ip1</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> </TC> </CCC> <CCC_NAME>ip2</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> <TC_LOC>/tftpboot/tc3</TC_LOC> <TC_LOC>/tftpboot/tc4</TC_LOC> </TC> </CCC> </COMPONENT> <COMPONENT> <COMP_NAME>parser</COMP_NAME> <CCC> <CCC_NAME>p1</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> <TC_LOC>/tftpboot/tc2</TC_LOC> </TC> </CCC> <CCC_NAME>p2</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> </TC> </CCC> <CCC_NAME>p3</CCC_NAME> <TC> <TC_LOC>/tftpboot/tc1</TC_LOC> <TC_LOC>/tftpboot/tc2</TC_LOC> <TC_LOC>/tftpboot/tc3</TC_LOC> </TC> </CCC> </COMPONENT> </CCCMAP>

and this is my code to generate a hash from this file
open (FILE, "< $mapfile") or die ("Unable to read the file $mapfile"); while (my $str = <FILE>) { chomp($str); $comp = $1 if $str =~ m{<COMP_NAME>(.*?)</COMP_NAME>}; $cmd = $1 if $str =~ m{<CCC_NAME>(.*?)</CCC_NAME>}; $tc = $1 if $str =~ m{<TC_LOC>(.*?)</TC_LOC>}; if ($tc) { push @tc, $tc; } if ( ($str =~ m{</TC>}) ) { $hoh{$comp}{$cmd} = {@tc}; #push @{$hoh{$comp}{$cmd}}, { @tc }; next; } elsif ( ($str =~ m{</CCC>})||($str =~ m{</COMPONENT>}) ) { undef @tc; undef $tc; next; } else { next; } } # print Dumper \%hoh;
and this is the output hash i get from this code
$VAR1 = { 'ip' => { 'ip1' => { '/tftpboot/tc1' => '/tftpboot/tc1' }, ' +ip2' => { '/tftpboot/tc4' => '/tftpboot/tc4', '/tftpboot/tc1' => '/tf +tpboot/tc3' } }, 'parser' => { 'p2' => { '/tftpboot/tc1' => '/tftpboo +t/tc1' }, 'p3' => { '/tftpboot/tc3' => '/tftpboot/tc3', '/tftpboot/tc +1' => '/tftpboot/tc2' }, 'p1' => { '/tftpboot/tc2' => undef, '/tftpbo +ot/tc1' => '/tftpboot/tc2' } } };
there is minor error in my code which makes this hash improper
the hash has to be like this
$VAR1 = { 'ip' => { 'ip1' => { '/tftpboot/tc1' }, 'ip2' => { '/tftpboo +t/tc4', '/tftpboot/tc1', '/tftpboot/tc3' } }, 'parser' => { 'p2' => { + '/tftpboot/tc1' }, 'p3' => { '/tftpboot/tc3', '/tftpboot/tc1', '/tft +pboot/tc2' }, 'p1' => { '/tftpboot/tc1' => '/tftpboot/tc2' } } };

please help me to fix this problem
thanks
rsennat

Edit: g0n - added readmore tags


In reply to problem parsing XML into hash by rsennat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.