如何使用Perl的XML::Twig修改适当的XML文件?

时间:2022-09-21 21:16:00

I am using Perl and XML::Twig. I have made some changes in the XML file( changing the values of the tags). But I cannot write it in the same XML file. If I use parsefile_inplace() the whole XML file got empty. Please find the code below.

我正在使用Perl和XML::Twig。我在XML文件中做了一些更改(更改标记的值)。但是我不能在同一个XML文件中编写它。如果我使用parsefile_inplace(),整个XML文件将为空。请找到下面的代码。

use strict;
use warnings;
use XML::Twig;

my $twig= XML::Twig->new(
        pretty_print => 'indented',
        twig_handlers => {
            jdk => sub{ 
            $_->set_text( 'JDK 1.8.0_40' )
            },
            },
        );
$twig->parsefile_inplace( 'nightly.xml', 'bak.*' );
$twig->flush;

Part of XML file:

XML文件的一部分:

<jdk>JDK 1.7.0_40</jdk>

If I use flush command it gives the desired output on cmd.

如果我使用flush命令,它会在cmd上给出所需的输出。

If I use parsefile_inplace command it is emptying the nightly.xml.

如果我使用parsefile_inplace命令,它正在清空night .xml。

If I just use parsefile the original file is left intact without changing the value i want to change.

如果我只是使用parsefile,那么原始文件将保持原样,不会更改我想要更改的值。

My requirement is to edit the value and save it in the same file

我的要求是编辑该值并将其保存到同一个文件中

1 个解决方案

#1


4  

Here is what the documentation for parsefile_inplace says:

parsefile_inplace的文档是这样说的:

parsefile_inplace ( $file, $optional_extension)

parsefile_inplace(文件,optional_extension美元)

Parse and update a file "in place". It does this by creating a temp file, selecting it as the default for print() statements (and methods), then parsing the input file. If the parsing is successful, then the temp file is moved to replace the input file.

解析和更新文件“到位”。它通过创建一个临时文件,选择它作为print()语句(和方法)的默认值,然后解析输入文件来实现这一点。如果解析成功,那么将迁移temp文件以替换输入文件。

If an extension is given then the original file is backed-up (the rules for the extension are the same as the rule for the -i option in perl). (emphasis mine)

如果给定一个扩展名,那么原始文件将被备份(扩展名的规则与perl中的-i选项的规则相同)。(强调我的)

I do not see any print statements in your code.

我在您的代码中没有看到任何打印语句。

Here is one way to do it, with small changes to your code:

这里有一种方法,对代码做一些小小的修改:

#!/usr/bin/env perl

use strict;
use warnings;

use XML::Twig;

my $twig= XML::Twig->new(
    pretty_print => 'indented',
    twig_roots => {
        jdk => sub{
            my ($t, $jdk) = @_;
            $jdk->set_text( 'JDK 1.8.0_40' );
            $jdk->print;
            $jdk->purge;
            return;
        },
    },
    twig_print_outside_roots => 1,
);

$twig->parsefile_inplace( 'nightly.xml', '.bak' );
$twig->flush;

#1


4  

Here is what the documentation for parsefile_inplace says:

parsefile_inplace的文档是这样说的:

parsefile_inplace ( $file, $optional_extension)

parsefile_inplace(文件,optional_extension美元)

Parse and update a file "in place". It does this by creating a temp file, selecting it as the default for print() statements (and methods), then parsing the input file. If the parsing is successful, then the temp file is moved to replace the input file.

解析和更新文件“到位”。它通过创建一个临时文件,选择它作为print()语句(和方法)的默认值,然后解析输入文件来实现这一点。如果解析成功,那么将迁移temp文件以替换输入文件。

If an extension is given then the original file is backed-up (the rules for the extension are the same as the rule for the -i option in perl). (emphasis mine)

如果给定一个扩展名,那么原始文件将被备份(扩展名的规则与perl中的-i选项的规则相同)。(强调我的)

I do not see any print statements in your code.

我在您的代码中没有看到任何打印语句。

Here is one way to do it, with small changes to your code:

这里有一种方法,对代码做一些小小的修改:

#!/usr/bin/env perl

use strict;
use warnings;

use XML::Twig;

my $twig= XML::Twig->new(
    pretty_print => 'indented',
    twig_roots => {
        jdk => sub{
            my ($t, $jdk) = @_;
            $jdk->set_text( 'JDK 1.8.0_40' );
            $jdk->print;
            $jdk->purge;
            return;
        },
    },
    twig_print_outside_roots => 1,
);

$twig->parsefile_inplace( 'nightly.xml', '.bak' );
$twig->flush;