in reply to Multiple XML files from Directory to One XML file using perl.

You are indeed picking an ambitious project for your first experience with the Perl language, but let me give you a website that you must bookmark:   http://search.cpan.org.

If you go to that site and type, “XML,” then ... oops! ... you get 5,000 hits.   So, here are a couple of searches to have a look at:

Your general approach will be to construct a new XML object representing your output file, and then to cycle through a directory (search e.g. for File::Find), open each one of the XML files you find there, and transfer nodes and subtrees from one to the other.

You may (erroneously, as it turns out) assume that you must write complicated code to navigate through the structure to see if things already exist, but you don’t:   that is what “XPath expressions” are for.   True XML difference-engines are also available.

Your strategy will partly depend on how large these files actually are.

You might also discover that there are altogether different ways to do it, such as XSLT, and existing engines such as Saxon.   In the desktop publishing world, DocBook is used to write documentation and books in an XML-based format, and books are actually assembled from hundreds or thousands of component parts using (a little hand-waving here ... what I am saying is not quite true...) no programming at all.   Because XML is now used so widely and in so many ways, the company of available tools is quite large and mature ... and, “Perl is there.”

Replies are listed 'Best First'.
Re^2: Multiple XML files from Directory to One XML file using perl.
by jyo (Initiate) on Nov 18, 2011 at 15:30 UTC

    Hi, Can any one provide some example scripts that add multiple XML files into One XML file

        Hi, I tried like this by using XML::LibXML::Reader

        #!/usr/bin/perl use warnings; use strict; use Carp; use File::Find; use File::Spec::Functions qw( canonpath ); use XML::LibXML::Reader; use Data::Dumper; my $INFO; my @ARGV ="C:/file/dir"; die "Need directories\n" unless @ARGV; find( sub { my $file = $_; #my $path = canonpath $File::Find::name; my $path =$_; return unless -f $path; return unless $file =~ /[.]xml\z/i; extract_information($path); return; }, @ARGV ); sub extract_information { my( $path)=@_; my $ret = open my $xmlin, '<', $path; unless ($ret) { carp "Cannot open '$path': $!"; return; } my $reader = XML::LibXML::Reader->new(IO => $xmlin); unless ($reader) { carp "Cannot create reader using '$path'"; return; } while ($reader->nextElement('shipto')) { $INFO = $reader->readOuterXml(); print "$INFO\n"; } close $xmlin or carp "Cannot close '$path': $!"; return; }

        but I have two problem in this script

        1) I am extracting information from all XML files Having "shiporder" Node element, But in one XML file I have data with some other Node element "definition" I am not extracting that information, What should I do if I want to extract that information and store in the same variable.

        2) After extracting all information That is stored in a $INFO varible, I want to store that $INFO variable information in one xml file how can I do that one. Please help me.