我有一个名为 Logs 的文件夹,其中包含多个文件夹 Run 1、Run 1 (1)、Run 2 (2) 等。每个文件夹都包含一个我需要解析的 plist 文件。但我无法打开文件。我正在运行以下代码:
my $count = 0;
my $path = "Logs/";
for my $file (glob("${path}*/*Results.plist")) {
# initialize parser object and find insances of Fail
my $xp = XML::XPath->new(filename=>$file);
my @nodeset = $xp->find('/dict/array/dict/string[1]');
foreach my $element (@nodeset){
if ($element->string_value == "Fail") {
$count++;
}
}
}
print $count;
编辑:现在我需要在“字符串”子节点中查找“失败”。plist 文件有 4 个失败,但我当前的代码只返回 2 个。有什么想法吗?
plist 文件的结构与此类似:
<plist version="1.0">
<dict>
<key>All Samples</key>
<array>
<dict>
<key>LogType</key>
<string>Fail</string>
<key>Message</key>
<string>Text</string>
<key>Timestamp</key>
<date>2012-10-17T08:01:51Z</date>
<key>Type</key>
<integer>4</integer>
</dict>
<dict>
<key>LogType</key>
<string>Fail</string>
<key>Message</key>
<string>An error occurred while trying to run the script.</string>
<key>Timestamp</key>
<date>2012-10-17T08:20:46Z</date>
<key>Type</key>
<integer>7</integer>
</dict>
<dict>
<key>LogType</key>
<string>Pass</string>
<key>Message</key>
<string></string>
<key>Timestamp</key>
<date>2012-10-17T08:01:51Z</date>
<key>Type</key>
<integer>5</integer>
</dict>
</array>
</dict>
</plist>