如何使用 XML Twig 将修改后的树保存到磁盘中

发布于 2024-09-15 02:45:46 字数 1160 浏览 3 评论 0原文

尝试了下面的方法,但我得到了 0 文件和此错误。 print() 在 C:/Perl/site/lib/XML/Twig.pm 第 3036 行的未打开文件句柄 OUT 上。

#!c:\perl\bin\perl.exe
use strict;
use XML::Twig;

my $upd_file = "updev.xml" ;
my $main_file = "main.xml" ;

# get the info we need by loading the update file
my $t_upd= new XML::Twig();

$t_upd->parsefile( $upd_file);

my $upd_dev_id = $t_upd->root->next_elt( 'DEVNUM')->text;
my $upd_dev    = $t_upd->root->next_elt( 'DEVS');
my $upd_seg    = $t_upd->root ;

# now process the main file
my $t= new XML::Twig( TwigHandlers => { DEVS => \&DEVS, },
              PrettyPrint => 'indented',
            );
$t->parsefile( $main_file);
$t->flush;           # don't forget or the last closing tags won't be printed

open( OUT, ">$main_file") or die "cannot open out file main_file:$!";

sub DEVS
  { my( $t, $DEVS)= @_;
    # just replace devs if the previous dev_id is the right one
    if( $DEVS->prev_elt( 'DEVNUM')->text eq $upd_dev_id) {
      $upd_dev->replace($DEVS);    
    }
     $t->flush(\*OUT) ;  # print and flush memory so only one dev is in there at once

  }

close OUT ;

Tried this below but I get a 0 file and this error.
print() on unopened filehandle OUT at C:/Perl/site/lib/XML/Twig.pm line 3036.

#!c:\perl\bin\perl.exe
use strict;
use XML::Twig;

my $upd_file = "updev.xml" ;
my $main_file = "main.xml" ;

# get the info we need by loading the update file
my $t_upd= new XML::Twig();

$t_upd->parsefile( $upd_file);

my $upd_dev_id = $t_upd->root->next_elt( 'DEVNUM')->text;
my $upd_dev    = $t_upd->root->next_elt( 'DEVS');
my $upd_seg    = $t_upd->root ;

# now process the main file
my $t= new XML::Twig( TwigHandlers => { DEVS => \&DEVS, },
              PrettyPrint => 'indented',
            );
$t->parsefile( $main_file);
$t->flush;           # don't forget or the last closing tags won't be printed

open( OUT, ">$main_file") or die "cannot open out file main_file:$!";

sub DEVS
  { my( $t, $DEVS)= @_;
    # just replace devs if the previous dev_id is the right one
    if( $DEVS->prev_elt( 'DEVNUM')->text eq $upd_dev_id) {
      $upd_dev->replace($DEVS);    
    }
     $t->flush(\*OUT) ;  # print and flush memory so only one dev is in there at once

  }

close OUT ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑕疵 2024-09-22 02:45:46

您的问题可能是您传递了文件句柄而不是文件句柄引用。

$t->flush(\*OUT) # look at the '\'

对您的代码的一些建议:在现代 Perl 中,最好使用三个参数打开和间接文件句柄:

open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' for writing: $!";

$twig->print($fh_out); # this prints to the filehandle

另一种打印方式是使用 $twig->sprint 对树进行字符串化,并像往常一样打印到文件句柄

 print {$fh_out} $twig->sprint();

Probably your problem is that you have passed the filehandle not a filehandle reference.

$t->flush(\*OUT) # look at the '\'

Some suggestions for your code: in modern perl is better to use the three arguments open and indirect filehandles:

open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' for writing: $!";

$twig->print($fh_out); # this prints to the filehandle

Another way of printing is stringify your tree with $twig->sprint and print to a filehandle as usual

 print {$fh_out} $twig->sprint();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文