jyo has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks, This Is my first Experience using perl language,I have Multiple xml files in a folder, I need to create multiple XML files into one XML file.example code like this
<!-------soc_foo.xml........!> <?xml version="1.0" encoding="UTF-8"?> <shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <shipto> <name>johan</name> <address>Langgt 23</address> <------more info--------> </shipto> </shiporder> <!-------tmm_foo.xml........!> <?xml version="1.0" encoding="UTF-8"?> <shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <shipto> <name>benny</name> <address>galve 23</address> <------more info--------> </shipto> </shiporder> <!-------svr_foo.xml........!> <?xml version="1.0" encoding="UTF-8"?> <shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <shipto> <name>kent</name> <address>vadrss 25</address> <------more info--------> </shipto> </shiporder>
Each xml file have same root and nodes with different data(name, address are different).I need to combine multile XML file into one XML file, my output look like this.
<!-------new.xml........!> <?xml version="1.0" encoding="UTF-8"?> <shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <shipto> <name>johan</name> <address>Langgt 23</address> <------more info--------> </shipto> <shipto> <name>benny</name> <address>galve 23</address> <------more info--------> </shipto> <shipto> <name>kent</name> <address>vadrss 25</address> <------more info--------> </shipto> </shiporder>
I Find the all xml file from directory using File::find But How can I Add all XML files data into one xml file without repeating same data. Its looking easy for You but I am very new to perl,help me with this problem. This is my first post, if any mistakes please excuse me.
For the above question I tried like this
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.
Thanks in advance
jyo
|
|---|