我在解析xml標籤的文件時遇到問題,問題是文件可能有許多xml標籤或者它可能只有一個。我試圖通過正則表達式和使用LibXML來做到這一點。正則表達式的問題是,如果有兩個在同一行中我的表現甚至打印第一標籤的開始之間的數據,直到第二次關閉標籤月底封閉式標籤解析具有多個或1個標籤的XML文件
xml文件 -
She outsprinted Becky Smith and Joan Hare to the line, with Becky and Joan
finishing in a time of <time>1:02:41</time> and <time> 1:02:45</time>
respectively.
正則表達式,我用(想拉時間的詳細信息) -
if (/<time>(.*)<\/time>/) {
($hh, $mm, $ss) = split(':', $1);
say "Time Entered - ", $hh, ":", $mm, ":", $ss, " ";
print "***$1***\n";
}
輸出
Time Entered - 1:02:41</time> and <time> 1
預期 -
1:02:41
1:02:45
**第二途徑探索 - **的libxml我 試圖與這下面的代碼,但它給了我一個錯誤說
"KnoxHalfResults:1: parser error : Start tag expected, '<' not found
Jim Colatis won Tuesday's Knoxville half marathon in a blistering pace"
輸入文件數據 - 這
Jim Colatis won Tuesday's Knoxville half marathon in a blistering pace
of <time> 0:56:45 </time>. He was followed to the line by long time nemesis
Mickey Mouse in a time of <time>0:58:49</time>.
my code for LibXML -
use warnings;
#use XML::Twig;
use XML::LibXML;
my $filein;
my $fileout;
($filein, $fileout) = @ARGV;
my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filein);
for my $sample ($xmldoc->findnodes('/time')) {
print $sample->nodeName(), ": ", $sample->textContent(), "\n";
}
與您的說法相反,這不是一個XML文件,這就是libxml抱怨的原因。 – ikegami