How to write to the same XML file at different times of the day?

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
 
M

Matt Garrish

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.

If you're looking for in-place updates you're likely going to be
disappointed. With a DOM parser, you could read in the file, operate on the
nodes (or add new ones) and then write new tree back to disk. A SAX parser
would let you read the one file and write to another as you process the new
data (but you'd have to decide how that becomes your master file again).
XML-Simple, however, will lose the order of your data, so I wouldn't
recommend it for anything but the most trivial of data reading tasks.

Matt
 
E

Eric J. Roode

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.

Isn't that what you said you wanted to do? :)


The short answer is, you do have to re-create it each time. You'll have to
read the old structure into a memory data structure, update it, then write
the whole shebang to the disk file at the end.

Unix and Windows filesystems don't know from XML, and have no concept of
"insert another element here between these two elements". They're just
streams of ASCII (or UTF-8, or whatever) characters. In order to insert
something, you have to shove everything down.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
S

Sherm Pendley

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.

Open the file for updating:

open OUTPUTXM, "+<$OutputFile" or die "could not update $OutputFile: $!";

Seek backwards from the end of the file, to the point where you'd overwrite
the first '<' in "</MSR>".

use Fcntl qw:)seek);
seek OUTPUTXM, -6, SEEK_END;

At this point it might not be a bad idea to do a sanity check - read a line
from the file and verify that it's "</MSR>".

Write out the new <Info>...</Info> element.

Remember, you're not inserting new data, you're *overwriting* what's already
there. So, you'll need to write a new "</MSR>" closing tag.

Relevant pods:

perldoc perlopentut
perldoc -f seek

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top