使用XML中的最新datetime更新节点属性 [英] update node attribute with latest datetime in XML

查看:196
本文介绍了使用XML中的最新datetime更新节点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:

< 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>

问:我的问题我需要处理这个XML文件并修改某些元素中的几个数据。我可以在这个网站的帮助下这样做,你可以从我以前的帖子中看到。

Q: My Problem I need to process this XML file and modify few data in certain Elements. I am able to do that with the help of this site as you can see from my previous posts.

有一个新的挑战。您可以看到XML中还有datetime元素。而且我需要以与XML中显示的格式相同的格式将其更新为当前的日期时间。即XML将始终包含任何日期,我只是为了找到datetime标签,并将其更新为当前的datetime,但它应该与它所在的格式相同。

There is a new challenge. as you can see there is datetime element in the XML as well. And I need to update this to the current datetime in the same format as it appears in the XML. i.e. the XML will always contain any date and I just to find the datetime tag and update it to the current datetime but it should be in the same format as it is in there.

我应该把它作为一个单独的问题,并将其视为一个简单的正则表达式Perl问题,并作为一个平面文件相应解决查找和替换问题

Shall I take it as separate issue and treat as simple regex Perl problem and resolve accordingly as a flat file find and replace problem?

LIBXML 内置任何内容来识别日期时间标签,并使用最新的datetime进行更新。

or LIBXML has anything built into to identify the datetime Tag and update it with latest datetime?

推荐答案

根据您的意思,没有什么特别的标识datetime标签。你当然可以寻找符合特定格式的属性,但这真的是你想要的?

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?

使用 XML :: LibXML Time :: Piece 将beginTime替换为当前日期:

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'\n";
        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>

要更新beginTime和endTime,您可以创建一个更复杂的xpath:

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'\n";
            my $newdate = localtime->strftime($date_format);
            $node->setAttribute($key, $newdate . $tz);
        }
    }
}

这篇关于使用XML中的最新datetime更新节点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆