I'm trying to generate a fresh XML file using Perl XML::Mini::Document
. It's working fine, but I don't know whether it's the right way to do it. Performance is the problem for me; it takes more time when the when record count increases.
Is there any other module better doing than this one in performance and easier way?
#!/usr/bin/perl -w
use warnings;
use XML::Mini::Document;
my $outfile = "D:/Test.xml";
my $nSequence = 1000;
my $sRandom_Name = "";
my $sRandom_Desc = "";
my $newDoc = XML::Mini::Document->new();
my $newDocRoot = $newDoc->getRoot();
my $xmlHeader = $newDocRoot->header('xml');
$xmlHeader->attribute('version', '1.0');
$xmlHeader->attribute('encoding', 'UTF-8');
my $records= $newDocRoot->createChild('records');
for(0..9) {
for(1..6) {
$sRandom_Name = $sRandom_Name.(chr(int(rand(25) + 65)));
}
for(1..15) {
$sRandom_Desc = $sRandom_Desc.(chr(int(rand(25) + 97)));
}
my $record = $records->createChild('record');
$record->createChild('ID')->text($nSequence=$nSequence+1);
$record->createChild('Name')->text($sRandom_Name);
$record->createChild('Desc')->text($sRandom_Desc);
print $newDoc->toFile($outfile);
}
My output should look like this one:
<?xml version="1.0" encoding="UTF-8" ?> <records> <record> <ID>1001</ID> <Name>ASDSDF</Name> <Desc>ASDFsdsdfcwefSC</Desc> </record> <record> <ID>1002</ID> <Name>KDFNND</Name> <Desc>WEFsdssccwefSC</Desc> </record> <record> <ID>1003</ID> <Name>PORJDX</Name> <Desc>XceFsdsdfcASmsd</Desc> </record> . . . </records>
toFile
is inside thefor
loop. That means the file is going to be overwritten ten times each time the program is run – Borodin May 12 at 12:05XML::Twig
: stackoverflow.com/questions/29009370/assembling-xml-in-perl – Sobrique May 12 at 12:09toFile
outside of the loop it says Out of memory! for1 million
records – lazy May 12 at 12:15