使用Strawberry Perl和Twig在Windows上清除目录树中所有文件中xml括号中的内容 [英] Clear content in xml brackets in all files in directory tree on Windows using Strawberry Perl and twig

查看:109
本文介绍了使用Strawberry Perl和Twig在Windows上清除目录树中所有文件中xml括号中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清除放置在目录树中XML文件中<loot> </loot>元素内部的全部内容.我正在Windows 64位上使用Strawberry Perl.

I want to clear whole content that is placed inside of <loot> </loot> elements in XML files in a directory tree. I am using Strawberry Perl for windows 64 bit.

例如此XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<monster name="Dragon"/>
<health="10000"/>
<immunities>
   <immunity fire="1"/>
</immunities>
<loot>
<item id="1"/>
  <item id="3"/>
      <inside>
        <item id="6"/>
      </inside>
  </item>
</loot>

更改后的文件应为:

<?xml version="1.0" encoding="UTF-8"?>
<monster name="Dragon"/>
<health="10000"/>
<immunities>
   <immunity fire="1"/>
</immunities>
<loot>
</loot>

我有此代码:

#!/usr/bin/perl
use warnings;
use strict;

use File::Find::Rule;
use XML::Twig;

sub delete_loot {
   my ( $twig, $loot ) = @_;
   foreach my $loot_entry ( $loot -> children ) {
      $loot_entry -> delete;
   }
   $twig -> flush;
}

my $twig = XML::Twig -> new ( pretty_print => 'indented', 
                              twig_handlers => { 'loot' => \&delete_loot } ); 

foreach my $file ( File::Find::Rule  -> file()
                                     -> name ( '*.xml' )
                                     -> in ( 'C:\Users\PIO\Documents\serv\monsters' ) ) {

    print "Processing $file\n";
    $twig -> parsefile_inplace($file); 
}

但是它只能正确编辑遇到的第一个文件,其余文件保持清晰(0 kb清晰文件)

But it edits correctly only the first file it meets and the rest files leaves clear (0 kb clear files)

推荐答案

The XML::Twig doc says that "Multiple twigs are not well supported".

如果查看树枝对象的状态(例如,使用Data :: Dumper),则会发现第一次运行与后续运行之间存在很大差异.看起来它认为已经被完全冲洗了(的确如此,因为在第一次运行期间已经进行了完全冲洗).后续文件可能没有更多可打印的内容,文件最终为空.

If you look at the state of the twig object (using Data::Dumper for example) you see a strong difference between the first and subsequent runs. It looks like it considers that is has been totally flushed already (which is true, as there was a complete flush during the first run). It probably has nothing more to print for the subsequent files and the file ends up empty.

在每个循环中重新创建树枝对象对我来说都是有效的:

Recreating the twig object at each loop worked for me:

#!/usr/bin/perl
use warnings;
use strict;

use File::Find::Rule;
use XML::Twig;

sub delete_loot {
   my ( $twig, $loot ) = @_;
   foreach my $loot_entry ( $loot -> children ) {
        $loot_entry -> delete;
    }
}

foreach my $file ( File::Find::Rule  -> file()
                                     -> name ( '*.xml' )
                                     -> in ( '/home/dabi/tmp' ) ) {

    print "Processing $file\n";
    my $twig = XML::Twig -> new ( pretty_print => 'indented', 
                                  twig_handlers => { loot => \&delete_loot, } ); 
    $twig -> parsefile($file); 
    $twig -> print_to_file($file);
}

此外,我不得不更改XML文件结构以对其进行处理:

Also, I had to change the XML file structure to have it processed:

<?xml version="1.0" encoding="UTF-8"?>
<monster name="Dragon">
<health value="10000"/>
<immunities>
   <immunity fire="1"/>
</immunities>
<loot>
<item id="1"/>
  <item id="3">
      <inside>
        <item id="6"/>
      </inside>
  </item>
</loot>
</monster>

这篇关于使用Strawberry Perl和Twig在Windows上清除目录树中所有文件中xml括号中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆