Hi every one,
Please excuse me if any grammatical mistakes is there.
I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this
file1:bvr.xml
<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<details>
<name>johan</name>
<address>Langgt 23</address>
---more info---
</details>
</specification>
file2:kvr.xml
<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<details>
<name>venu</name>
---more info---
</details>
</specification>
file3:svr.xml
<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<details>
<name>kent</name>
---more info----
</details>
</specification>
file4:tvr.xml
<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<definition>
<name>kent</name>
----more info---
</definition>
</specification>
I need to create one xml file like this.
new.xml
<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<details>
<name>johan</name>
<address>Langgt 23</address>
--more info--
</details>
<details>
<name>venu</name>
---more info----
</details>
<details>
<name>kent</name>
<address>vadrss 25</address>
---more info--
</details>
<definition>
<name>kent</name>
----more info----
</definition>
</specification>
I tried like this But I have some problems with this script.
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
use XML::LibXML::Reader;
use Data::Dumper;
my $Number;
my $dir="C:/file/sav";
find(\&wanted, $dir);
sub wanted() {
if ( -f and /(\.xml)$/) {# find all the files with a suffix of .xml
my $reader = XML::LibXML::Reader->new( location =>$_ )
or die "cannot read file.xml\n";
while ($reader->nextElement( 'details' ) ){
$Number = $reader->readOuterXml();
print "$Number\n";
}
}
return;
}
but I have two problem in this script
1) I am extracting information from all XML files Having "details" 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 $Number
variable, I want to store that $Number
variable information in one XML file how can I do that one. Please help me.I am very new to perl.
(or)
Is there any other ways to combined all XML files data into one XML file.
After trying your code as you said it gives some more errors
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;
my @xml_files = glob '*.xml';
my $bigXML = XML::LibXML::Document->new( '1.0', 'UTF-8');
my $aggregated; for my $xml_file ( @xml_files ) {
my $doc = XML::LibXML->new->parse_file( $xml_file );
my ( $specNode ) = $doc->findnodes( '//Specification' );
if ( $aggregated ) {
$aggregated = $specNode;
}
else {
my @details = $specNode->findnodes( './details' );
$aggregated->addChild( $_ ) foreach @details;
}
}
$bigXML->adoptNode( $aggregated );
$bigXML->toFile( 'aggregated_data.xml' );
it giving same error as I asked before can't call method "findnodes" on an undefined value
, I tried to replce dot with slash in details line also it giving same error.
and if tried to use golb function like this
my @xml_files = glob '*dtc.xml';
in the above code this time it gives different error like
XML::LibXML::Document::adoptNode()-- Node is not a blessed SV reference at line 21
I am struggling with these errors because this is very new module for me and also I looked into the that document , every thing is in right format but I cant understand this errors. I am very beginner to this perl , can help me.
First of all, thank you for posting your effort here and for stating what you ultimately want to do, which is combine all XMLs into a single file.
To address the needs of the problem :
use strict; use warnings;
To catch silly mistakes and enforce good error handling
use XML::LibXML;
Use your parser
my @xml_files = glob '*.xml';
Find all XML files in your directory
my $bigXML = XML::LibXML::Document->new( '1.0', 'UTF-8');
Instantiate big XML where all nodes will be stored.
Loop over each file, get the node and push the node into an aggregated node
my $aggregated; for my $xml_file ( @xml_files ) {
my $doc = XML::LibXML->new->parse_file( $xml_file );
my ( $specNode ) = $doc->findnodes( '//specification' );
if ( ! $aggregated ) { # Initialize if doesn't exist
$aggregated = $specNode;
}
else { # Add more <details>
my @details = $specNode->findnodes( './details' );
$aggregated->addChild( $_ ) foreach @details;
}
}
Add the $aggregated
node to the $bigXML
document and print:
$bigXML->addChild( $aggregated );
$bigXML->toFile( 'aggregated_data.xml' );