#this variable will hold very large complex data structure. my $tableDetailsRef = {}; my $xmlFile = "test_name.xml"; my $targetNameStr = "target-name"; #Creating object for xml file my $xmls = XML::Simple->new( RootName => undef, ForceArray => ['tables'], KeyAttr => { table => 'id' }, ); my $contents = {}; $contents->{dspl}->{targetNamespace} = "string_test"; ################################################ if(defined $tableDetailsRef){ push @{ $contents->{dspl}->{'tables'} }, { table => $tableDetailsRef, }; } #################################################### ##Generate the xml file eval { $xmls->XMLout( $contents, xmldecl => '', OutputFile => $xmlFile ); }; if($@) { print "Exception: Could not xml file : $xmlFile, Error-$@"); }