R
richardkreidl
How do I update a XML without having to re-create it every time I run
this script. The code below works great, except I just realized that
everytime I run the script it creates a new 'Output.xml' file over
writing the existing data from the prior run of the script.
This does not work:
open OUTPUTXML, ">>$OutputFile" or die "could not write to file: $!";
It just adds to the existing file.
The script inputs system time in certain XML elements during different
times of the day.
As you can see from these two lines of code in the script:
$xml_ar->{Info}{SIVU} = $dt; # this updates the "SIVU" element in
the XML file
#$xml_ar->{Info}{SSEC} = $dt;
Then if I run it again like this:
#$xml_ar->{Info}{SIVU} = $dt;
$xml_ar->{Info}{SSEC} = $dt; # this updates the "SSEC" element in
the XML file
How do I write to the same XML "Output.xml" file each time and not
erase any data from previous runs??
I hope I'm clear on explaining the question.
#!/usr/bin/perl
use XML::Simple;
my $InputFile = "/IS/Operations/MSR_XML_HTML/Input.xml";
my $OutputFile = "/IS/Operations/MSR_XML_HTML/Output.xml";
open INPUTXML,"<$InputFile" or die "Could not read file $!";
open OUTPUTXML, ">$OutputFile" or die "could not write to file: $!";
my $self = bless {}, "main";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
my $dt = "";
$dt .= ($hour>12) ? $hour - 12 : $hour;
$dt .= sprintf ":%02d", $min;
$dt .= ($hour>12) ? 'pm' : 'am';
$dt .= " Saturday" if $wday == 6;
my $xml = "";
{local $/; $xml .= <INPUTXML>; }
my $xml_ar = new XML::Simple->XMLin($xml);
$xml_ar->{Info}{SIVU} = $dt;
#$xml_ar->{Info}{SSEC} = $dt;
print OUTPUTXML XMLout($xml_ar, NoAttr=>1, RootName=>'MSR');
close OUT;
Input.xml file
<?xml version="1.0" standalone="yes"?>
<MSR>
<Info>
<TODAY>Monday, December 12, 2005</TODAY>
<BT30004>8:31pm</BT30004>
<LASTISA>12:21am Saturday</LASTISA>
<CM10009 />
<SSEC />
<SIVU />
<LASTSRM1 />
<LASTSRM1 />
</Info>
</MSR>
thanks
this script. The code below works great, except I just realized that
everytime I run the script it creates a new 'Output.xml' file over
writing the existing data from the prior run of the script.
This does not work:
open OUTPUTXML, ">>$OutputFile" or die "could not write to file: $!";
It just adds to the existing file.
The script inputs system time in certain XML elements during different
times of the day.
As you can see from these two lines of code in the script:
$xml_ar->{Info}{SIVU} = $dt; # this updates the "SIVU" element in
the XML file
#$xml_ar->{Info}{SSEC} = $dt;
Then if I run it again like this:
#$xml_ar->{Info}{SIVU} = $dt;
$xml_ar->{Info}{SSEC} = $dt; # this updates the "SSEC" element in
the XML file
How do I write to the same XML "Output.xml" file each time and not
erase any data from previous runs??
I hope I'm clear on explaining the question.
#!/usr/bin/perl
use XML::Simple;
my $InputFile = "/IS/Operations/MSR_XML_HTML/Input.xml";
my $OutputFile = "/IS/Operations/MSR_XML_HTML/Output.xml";
open INPUTXML,"<$InputFile" or die "Could not read file $!";
open OUTPUTXML, ">$OutputFile" or die "could not write to file: $!";
my $self = bless {}, "main";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
my $dt = "";
$dt .= ($hour>12) ? $hour - 12 : $hour;
$dt .= sprintf ":%02d", $min;
$dt .= ($hour>12) ? 'pm' : 'am';
$dt .= " Saturday" if $wday == 6;
my $xml = "";
{local $/; $xml .= <INPUTXML>; }
my $xml_ar = new XML::Simple->XMLin($xml);
$xml_ar->{Info}{SIVU} = $dt;
#$xml_ar->{Info}{SSEC} = $dt;
print OUTPUTXML XMLout($xml_ar, NoAttr=>1, RootName=>'MSR');
close OUT;
Input.xml file
<?xml version="1.0" standalone="yes"?>
<MSR>
<Info>
<TODAY>Monday, December 12, 2005</TODAY>
<BT30004>8:31pm</BT30004>
<LASTISA>12:21am Saturday</LASTISA>
<CM10009 />
<SSEC />
<SIVU />
<LASTSRM1 />
<LASTSRM1 />
</Info>
</MSR>
thanks