I am new to xml twig. I have an xml file (sample)

<violation id="52520" module="SIpcCam1c2r2w.vs" line="-1" message="Naming - Design file" key="" severity=" Error_L2" group="names" hierarchy="iicb4" design_id="iicb4" critical="yes" waiver="" />

I am trying to read each line and then output after rearranging into another xml file

<waiver descr="Waiver_52520" owner="xxx" end_date="2020-12-31" open_date="Tue May 8 2010" limit="-1" id="iicb4" rule="52520" file=".*/SIpcCam1c2r2w.vs" key="Naming - Design file" />

I am trying with twig_root and twig handler like this:

$in_twig->parsefile('in.xml'); $in_twig = XML::Twig->new( twig_roots => { violation => 1 }, twig_handlers => { violation => sub { processViolation( $in_twig, $_, +\%out_twigs, \%opt ); } ); sub processViolation { my $in_twig = shift( @_ ); my $violation = shift( @_ ); my $out_twigs_href = shift( @_ ); my $opt_href = shift( @_ ); printf "Entered subroutine\n"; my $att_waiver = $violation->att('waiver'); if( !defined( $att_waiver ) || ($att_waiver eq "") ) { my $file = ".*/". basename( $violation->att('module') ); my $rule = $violation->att('id'); my $id = join( '.', basename( $violation->att('module') ), $opt_href->{idsid}, $violation->att('key') ); $id =~ s/\s+//g; my $open_date = localtime(); my $waiver = XML::Twig::Elt->new( "waiver", { descr => "", id => $id, owner => $opt_href-> +{idsid}, rule => $rule, file => $file, key => $violation->a +tt('key'), message => '.*', open_date => $open_d +ate } ); print $waiver->sprint() ."\n"; if( !exists( $out_twigs_href->{$rule} ) ) { $out_twigs_href->{$rule}->{sev} = $violation->att('severity'); $out_twigs_href->{$rule}->{twig} = XML::Twig->new( pretty_print +=> 'indented' ); $out_twigs_href->{$rule}->{twig}->set_encoding( 'UTF-8' ); $out_twigs_href->{$rule}->{twig}->set_root( XML::Twig::Elt->new( + 'LINTRA_WAIVERS' ) ); } $waiver->paste( 'last_child', $out_twigs_href->{$rule}->{twig}->ro +ot() ); } $in_twig->purge(); return( 1 ); }

I got a basic code and modified it but it isn't working. I am not able to see $waiver at all. Would really appreciate if someone can help.


In reply to new user of xml::twig by springleaf

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.