PayPal is the easiest way to receive money online. Get one now or Click here to sign up. Sign up for PayPal and start accepting credit card payments instantly.

How to tidy XML file

Below is the simple perl code that will clean up the xml file. You can use XML::Tidy library.

use strict;
use XML::Tidy;

use Win32;


if ($ARGV[0] eq "") {
print "XML file is missing\n\t xmltidy.exe \"File.xml\"\n"
}else
{

if ($ARGV[0]!~ /.xml$/){print "XML file is missing\n\t xmltidy.exe \"File.xml\"\n";exit;}
my $xml=$ARGV[0];
if (!(-f $ARGV[0])) {$xml=Win32::GetCwd().'\\'.$ARGV[0]} ;
# create new XML::Tidy object from MainFile.xml
my $tidy_obj = XML::Tidy->new('filename' => $xml);
# Tidy up the indenting
$tidy_obj->tidy();
# Write out changes back to MainFile.xml
$tidy_obj->write();

}

Simple Perl Tidy.

This code below will tidy your perl code.

use File::Copy;
opendir (DIR , Win32::GetCwd());
my @DIR = readdir(DIR);
closedir DIR;

my $tabValues = ' ';
my $FileToClean ='myperlfile.pl';
#foreach(@DIR)
{
# my $FileToClean ='myperlfile.pl';
#$FileToClean =$_;
# next if ($FileToClean =~ /\.bak$/gi);
copy ($FileToClean,"$FileToClean.bak");
open(FILE ,"$FileToClean");
my $templine;
my $tab;
while(my $line = )
{
$line =&trim($line);
#print $line."\r\n";
if ($line =~ /.*TOVALIDDATE.*/gi)
{
print "test\n";
}
if ($line ne "")
{
if ($line=~ /.*\(.*\)\{.*\}/gi)
{
$templine .= $tab.$line."\r\n";
next;
}
if ($line=~ /(^(while|if|else|elsif|foreach).*\(.*\).*)(\{.*)/gi)
{

$templine .= $tab.$1."\r\n";
$templine .= $tab.$3."\r\n";
$tab .= $tabValues;
next;
}
if($line=~ /^#/gi)
{
$templine .= $tab.$line."\r\n";
next;
}
if($line=~ /^{/gi || $line=~ /^(while|if|else|elsif|foreach).*{$/gi)
{
$templine .= $tab.$line."\r\n";
$tab .= $tabValues;
next;
}
if($line=~ /^}/gi || $line=~ /^}.*(while|if|else|elsif|foreach).*/gi)
{
$tab =~ s/$tabValues//;
if($line=~ /^{/gi || $line=~ /.*(while|if|else|elsif|foreach).*{/gi)
{
$tab .= $tabValues;
}
$templine .= $tab.$line."\r\n";
next;
}
if($line=~ /}$/gi || $line=~ /.*(while|if|else|elsif|foreach).*}$/gi)
{
$tab =~ s/$tabValues//;
#$line =~ s/}/$tab}/;
$templine .= $tab.$line."\r\n";
next;
}
if($line=~ /^sub .*/gi)
{
$tab="";
$templine .= $tab.$line."\r\n";
my $aaa= trim($line);
if($aaa=~ /^sub .*{$/gi)
{
$tab=$tabValues;
}
next;
}

$templine .= $tab.$line."\r\n";
}
}
close FILE;
open(FILEOUT,">$FileToClean");
print FILEOUT $templine;
close FILEOUT;
}
exit;
sub trim
{
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
return $str;
}

WebProNews Feed

eWeek - RSS Feeds

HowtoForge - Howtos and Tutorials

The Register

PayPal is the easiest way to receive money online. Get one now or Click here to sign up. Sign up for PayPal and start accepting credit card payments instantly.