Perl使用困难的Regex将String从XML写入File

Mar*_*ley 2 regex perl

我有一个XML文件,我需要使用Perl传输到列表(不使用XSLT).

这是我的(简单,删除了10个以上的属性,以便于阅读!)XML:

...
<XMLTAG ID="1" name="NAME1" status="0" date1="24.05.2012 13:37:00" date2="25.05.2012 13:37:00" />
<XMLTAG ID="2" name="NAME2" status="1" date1="24.05.2012 13:37:00" date2="25.05.2012 13:37:00" />
<XMLTAG ID="3" name="NAME3" status="0" date1="24.05.2012 13:37:00" date2="25.05.2012 13:37:00" />
...
Run Code Online (Sandbox Code Playgroud)

到目前为止我得到了什么:

my $input = in.xml;
my $output = out.txt;

# open input
open( INPUT, $input )
  || die "Can't find $input: $_";

# open output
open( OUTPUT, ">$output" )
  || die "Can't find $output: $_";

    # run until perl returns undef (at the end of the file)
    while (<INPUT>) {
        if ($_ == /date1=\"[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-5][0-9]:[0-5][0-9]:[0-5][0-9]\"/) {
        print OUTPUT $_;};
    }
    close(INPUT);
    close(OUTPUT);
Run Code Online (Sandbox Code Playgroud)

输出文件应如下所示:

date1="24.05.2012 13:37:00"
date1="24.05.2012 13:37:01"
date1="24.05.2012 13:37:02"
...
Run Code Online (Sandbox Code Playgroud)

谢谢,马利

dax*_*xim 6

use XML::LibXML qw();
my $dom = XML::LibXML->load_xml(location => 'in.xml');
printf qq(date1="%s"\n), $_->getAttribute('date1')
    for $dom->findnodes('//XMLTAG');
Run Code Online (Sandbox Code Playgroud)