I have multiple XML files in a folder,so I written script like this to combine into one xml file
#!/usr/bin/perl
use warnings;
use XML::LibXML;
use Carp;
use File::Find;
use File::Spec::Functions qw( canonpath );
use XML::LibXML::Reader;
use Digest::MD5 'md5';
if ( @ARGV == 0 ) {
push @ARGV, "c:/main/work";
warn "Using default path $ARGV[0]\n Usage: $0 path ...\n";
}
open( my $allxml, '>', "all_xml_contents.combined.xml" )
or die "can't open output xml file for writing: $!\n";
print $allxml '<?xml version="1.0" encoding="UTF-8"?>',
"\n<Shiporder xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
my %shipto_md5;
find(
sub {
return unless ( /(_stc\.xml)$/ and -f );
extract_information();
return;
},
@ARGV
);
print $allxml "</Shiporder>\n";
sub extract_information {
my $path = $_;
if ( my $reader = XML::LibXML::Reader->new( location => $path )) {
while ( $reader->nextElement( 'data' )) {
my $elem = $reader->readOuterXml();
my $md5 = md5( $elem );
print $allxml $reader->readOuterXml() unless ( $shipto_md5{$md5}++ );
}
}
return;
}
It printing all xml files into one xml like this.
all_xml.combined.xml
<?xml version="1.0" encoding="UTF-8"?>
<student specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<student>
<name>johan</name>
</student>
<student>
<name>benny</name>
</student>
<student>
<name>kent</name>
</student>
</student specification>
but I have one more node information in one xml file, i tried to extract that information like this in while loop.
$reader->nextElement( 'details' );
$information = $reader->readInnerXml();
but how can i add this information to output file, please help me with this problem.
Three obvious points.