Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

new user of xml::twig

by springleaf (Initiate)
on Aug 02, 2012 at 23:45 UTC ( #985120=perlquestion: print w/replies, xml ) Need Help??
springleaf has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: new user of xml::twig
by Anonymous Monk on Aug 03, 2012 at 00:00 UTC

      Thanks for your reply. I could not open the examples link. Also what I want to do is basically read an xml, build a twig, access a certain twigroot and then rearrange data in another xml file. If you show me a sample code, it'll be greatly appreciated.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985120]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2017-08-23 04:32 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (345 votes). Check out past polls.