Perl XML:简单读取xml对并在每个对上执行某些操作

时间:2011-10-13 15:56:13

标签: xml perl parsing xml-simple

我有一个XML文件,其中包含以下部分:

<cptasks>
 <copy file="file1.txt" to="folder1/folder21" />
 <copy file="file2.txt" to="folder1/folder33" />
 <copy file="file3.txt" to="folder1/folder4" />
 <copy file="file4.txt" to="folder1/folder1" />
</cptasks>

我只需解析这个特定部分中的文件/对,并对它们运行一个函数 - 即

some_func_name(<file_value>,<to_value>)

我该怎么做?

提前谢谢你;

1 个答案:

答案 0 :(得分:2)

use XML::Simple;
my $cptasks = XMLin(qq~
<cptasks>
<copy file="file1.txt" to="folder1/folder21" />
<copy file="file2.txt" to="folder1/folder33" />
<copy file="file3.txt" to="folder1/folder4" />
<copy file="file4.txt" to="folder1/folder1" />
</cptasks>
~
);


for my $copy (@{$cptasks->{copy}}) {
    some_func_name($copy->{file}, $copy->{to});
}