#!/usr/bin/perl use strict; use warnings; use XML::Twig::XPath; my $file = "T:/BI/Jerome/xml/Portage.xml"; my $t= XML::Twig::XPath->new( twig_roots => { Job => \&job}, pretty_print => 'indented'); $t->parsefile( $file ); print_to_file('T:/BI/Jerome/xml/traite.xml'); exit; sub job { my ($t, $job)= @_; my @sub_record_table= $job->findnodes( './/SubRecord[./Property[@Name="Name" and text()="TABLE"]]'); return unless( @sub_record_table); my $out_job= XML::Twig::Elt->new( job => { identifier => $job->att( 'Identifier') }); foreach my $sub_record ( @sub_record_table) { my $table = $sub_record->field( 'Property[@Name="Value"]'); $out_job->insert_new_elt( last_child => TABLE => $table); my $found_sql=0; while( !$found_sql) { $sub_record= $sub_record->next_sibling( 'SubRecord') or last; my $tag= $sub_record->field( 'Property[@Name="Name"]'); my $content= $sub_record->field( 'Property[@Name="Value"]'); #if( $content ne 'No') # { $found_sql=1; # $out_job->insert_new_elt( last_child => $tag => $content); # } if ( $tag =~ /USERSQL/) { $found_sql = 1; $out_job->insert_new_elt( last_child => $tag => $content); } } } $out_job->print(); $t->purge; # if you need to free the memory }