There isn't anything special to identify a "datetime" tag as you mean it. You certainly could look for attributes that fit a specific format, but is that really what you want?
Using XML::LibXML
and Time::Piece
to replace the beginTime with the current date:
use strict;
use warnings;
use XML::LibXML;
use Time::Piece;
my $date_format = '%Y-%m-%dT%H:%M:%S';
my $data = do { local $/; <DATA> };
my $dom = XML::LibXML->load_xml(string => $data);
for my $node ($dom->findnodes('//*[@beginTime]')) {
if ( my $begin = $node->getAttribute('beginTime') ) {
my ($plaindate, $tz) = split '(?=+)', $begin;
my $date = Time::Piece->strptime($plaindate, $date_format);
print "old beginTime = '$date'
";
my $newdate = localtime->strftime($date_format);
$node->setAttribute('beginTime', $newdate . $tz);
}
}
print $dom->toString();
__DATA__
<measCollecFile>
<fileHeader>
<measCollec beginTime="2013-03-14T12:10:00+00:00" />
</fileHeader>
<measData>
<measInfo>
<granPeriod duration="PT300S" endTime="2013-03-14T12:15:00+00:00" />
<measType>VS.ave</measType>
<measType>VS.aveCPU</measType>
<measValue>VS1</measValue>
<measValue>VS2</measValue>
</measInfo>
</measData>
</measCollecFile>
To update both beginTime and endTime, you could create a more complicated xpath:
my @attribs = qw(beginTime endTime);
my $attribs_list = join ' | ', map {'@'.$_} @attribs;
for my $node ($dom->findnodes("//*[$attribs_list]")) {
for my $key (@attribs) {
if ( my $att = $node->getAttribute($key) ) {
my ($plaindate, $tz) = split '(?=+)', $att;
my $date = Time::Piece->strptime($plaindate, $date_format);
print "old $key = '$date'
";
my $newdate = localtime->strftime($date_format);
$node->setAttribute($key, $newdate . $tz);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…