您好,我正在尝试使用TWIG模块删除文件中的某些节点并打印新文件。它是2Go字节文件,所以我尝试逐块解析XML,但似乎我使用的方法不起作用,因为文件XML最终损坏,这是因为刷新导致无法读取所有文件
此处是代码
#------------------------------------------------------------------------------
# This function is to add a twig handler to remove the node in parameter
#------------------------------------------------------------------------------
sub delete_node {
#------------------------------------------------------------------------------
my ($node_to_delete,$file_to_read) = @_;
copy($file_to_read,$file_to_read.'-tmp_'.$dateLog.'.xml')
open my $file_end, '>', $file_to_read.'-parsed_'.$dateLog.'.xml'
my $handlers = {$node_to_delete => section_delete_node($file_end)};
my $twig = new XML::Twig(pretty_print => 'indented',
twig_handlers => $handlers,)->parsefile($file_to_read);
close $file_end;
if (-s $file_to_read.'-parsed_'.$dateLog.'.xml') {
move($file_to_read.'-parsed_'.$dateLog.'.xml', $file_to_read);
}
else {
move($file_to_read.'-tmp_'.$dateLog.'.xml', $file_to_read);
}
}
#------------------------------------------------------------------------------
# This function remove the node in parameter
#------------------------------------------------------------------------------
sub section_delete_node {
my ($file_end)= @_;
return sub {
my( $t, $section)= @_;
$section->delete();
$t -> flush($file_end);
}
}
例如,我要删除文件中的节点dob
<?xml version="1.0" encoding="UTF-8"?>
<record category="B" editor="" entered="2000-12-04" sub-category="PEP" uid="7320" updated="2018-12-12">
<person ssn="" e-i="E">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<names>
<first_name/>
<last_name>BA</last_name>
</names>
<agedata>
<age xsi:nil="true"/>
<as_of_date xsi:nil="true"/>
<dob xsi:nil="true"/>
<dobs>
<dob xsi:nil="true"/>
</dobs>
<deceased xsi:nil="true"/>
</agedata>
</person>
<details>
<id_numbers>
<id loc="INT" type="">fd</id>
</id_numbers>
<place_of_birth xsi:nil="true"/>
<locations>
<location country="df" city="re" state="">Shahrara</location>
</locations>
</details>
</record>
这里是结果
<?xml version="1.0" encoding="UTF-8"?>
<record category="B" editor="" entered="2000-12-04" sub-category="PEP" uid="7320" updated="2018-12-12">
<person ssn="" e-i="E">
<title xsi:nil="true"/>
<position xsi:nil="true"/>
<names>
<first_name/>
<last_name>BA</last_name>
</names>
<agedata>
<age xsi:nil="true"/>
<as_of_date xsi:nil="true"/>
<deceased xsi:nil="true"/>
</agedata>
</person>
</record>
<details>
<id_numbers>
<id loc="INT" type="">fd</id>
</id_numbers>
<place_of_birth xsi:nil="true"/>
<locations>
<location country="df" city="re" state="">Shahrara</location>
</locations>
</details>
如果您有帮助,那就太好了。非常感谢