使用XML中的datetime更新节点文本

时间:2014-04-09 09:52:39

标签: xml perl datetime libxml2

我有XML:

< measCollecFile>
    < fileHeader>
        < measCollec>2013-03-14T12:10:00+00:00</measCollec>
    </ fileHeader>
    < measData>
        < measInfo>
            < granPeriod>2013-03-14T12:15:00+00:00</granPeriod>
            < measType>VS.ave</measType>
            < measType>VS.aveCPU</measType>
            < measValue>VS1</measValue>
            < measValue>VS2</measValue>
        </ measInfo>
            < granPeriod>2013-03-14T12:15:00+00:00</granPeriod>
    </ measData>
</ measCollecFile>

我确实问similar query earlier并得到了米勒的答复。这略有不同。这次我需要更新节点上的日期时间而不是属性。

提供的节点列表qw(measCollec granPeriod)。我们需要以相同的格式更新当前日期时间。

1 个答案:

答案 0 :(得分:0)

最后,我能够回答我自己的问题。信用还在于米勒!!

 my @attribs = qw(measCollec granPeriod);

 my $attribs_list = join ' | ', map {$_} @attribs;

 NODE: for my $node ($dom->findnodes("//*[$attribs_list]")) {

    for my $key (@attribs) {
        my @titles = $dom->getElementsByTagName($key);
            foreach my $t (@titles) {

            my ($plaindate, $tz) = split '(?=\+)', $t->textContent;
            my $date = Time::Piece->strptime($plaindate, $date_format);
            print "old $key = '$date'\n";
            my $newdate = localtime->strftime($date_format);

                my $newrNode=XML::LibXML::Element->new($key);
                $newrNode->addChild($dom->createTextNode($newdate));
                $node->replaceChild($newrNode,$t);

        }
    }
last NODE; # ending the loop otherwise it will cascade again.

}

print $dom->toString();