Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找和替换XML标记之间的字符_Xml_Perl - Fatal编程技术网

查找和替换XML标记之间的字符

查找和替换XML标记之间的字符,xml,perl,Xml,Perl,我有一个不受行绑定的XML文件。它有标签和,其中包含一些来自生成它的代码的垃圾变量(我现在无法更正)。我希望能够更改这些标记中的字符以更正它们。人物有时很特别 我有一个Perl one liner来显示标记之间的内容,但现在我希望能够在文件中替换找到的内容 perl -0777 -ne 'while (/(?<=perform_cnt).*?(?=\<\/perform_cnt)/s) {print $& . "\n"; s/perform_cnt.*?\<\

我有一个不受行绑定的XML文件。它有标签
,其中包含一些来自生成它的代码的垃圾变量(我现在无法更正)。我希望能够更改这些标记中的字符以更正它们。人物有时很特别

我有一个Perl one liner来显示标记之间的内容,但现在我希望能够在文件中替换找到的内容

perl -0777 -ne 'while (/(?<=perform_cnt).*?(?=\<\/perform_cnt)/s) {print $& . "\n";      s/perform_cnt.*?\<\/perform_cnt//s}' output_error.txt
我需要将它们替换为0。

使用正则表达式进行xml解析是一种不好的做法 无论如何,代码是:

#!/usr/bin/perl

use strict;
use warnings;

my $tag = 'perform_cnt';

open my $fh, '<file.txt' or die $!;
foreach (<$fh>) {
  s/(<$tag>)(.*?)(<\/$tag>)/$1$3/g;
  print "$_";
}
close $fh;
<text1>120105728</text1><perform_cnt></perform_cnt>
<text1>120106394</text1><perform_cnt></perform_cnt>
#/usr/bin/perl
严格使用;
使用警告;
my$tag='perform_cnt';
打开我的$fh,“使用正则表达式进行xml解析是一种糟糕的做法
无论如何,代码是:

#!/usr/bin/perl

use strict;
use warnings;

my $tag = 'perform_cnt';

open my $fh, '<file.txt' or die $!;
foreach (<$fh>) {
  s/(<$tag>)(.*?)(<\/$tag>)/$1$3/g;
  print "$_";
}
close $fh;
<text1>120105728</text1><perform_cnt></perform_cnt>
<text1>120106394</text1><perform_cnt></perform_cnt>
#/usr/bin/perl
严格使用;
使用警告;
my$tag='perform_cnt';
打开我的$fh,“我喜欢这类东西。这需要一点时间来适应,但一旦您了解了设计(以及一点DOM处理),许多事情就会变得非常简单:

use XML::Twig;

my $xml = <<'HERE';
<root>
<text1>120105728</text1><perform_cnt>ÈPm=</perform_cnt>
<text1>120106394</text1><perform_cnt>†AQ;4K\_Ô23{YYÔ@Nx</perform_cnt>
</root>
HERE

my $twig = XML::Twig->new(   
    twig_handlers => { 
        perform_cnt   => sub { 
            say "Text is " => $_->text;  # get the current text

            $_->set_text( 'Buster' );    # set the new text
            },
      },
    pretty_print => 'indented',
    );

$twig->parse( $xml );
$twig->flush; 
使用XML::Twig;
我的$xml={
执行_cnt=>sub{
说“Text is”=>$\->Text;#获取当前文本
$u->set_text('Buster');#设置新文本
},
},
漂亮的打印=>“缩进”,
);
$twig->parse($xml);
$twig->flush;
使用缩进漂亮的打印,我得到:

<root>
  <text1>120105728</text1>
  <perform_cnt>Buster</perform_cnt>
  <text1>120106394</text1>
  <perform_cnt>Buster</perform_cnt>
</root>

120105728
巴斯特
120106394
巴斯特
我喜欢这类东西。这需要一点时间来适应,但一旦您了解了设计(以及一点DOM处理),许多事情就会变得非常简单:

use XML::Twig;

my $xml = <<'HERE';
<root>
<text1>120105728</text1><perform_cnt>ÈPm=</perform_cnt>
<text1>120106394</text1><perform_cnt>†AQ;4K\_Ô23{YYÔ@Nx</perform_cnt>
</root>
HERE

my $twig = XML::Twig->new(   
    twig_handlers => { 
        perform_cnt   => sub { 
            say "Text is " => $_->text;  # get the current text

            $_->set_text( 'Buster' );    # set the new text
            },
      },
    pretty_print => 'indented',
    );

$twig->parse( $xml );
$twig->flush; 
使用XML::Twig;
我的$xml={
执行_cnt=>sub{
说“Text is”=>$\->Text;#获取当前文本
$u->set_text('Buster');#设置新文本
},
},
漂亮的打印=>“缩进”,
);
$twig->parse($xml);
$twig->flush;
使用缩进漂亮的打印,我得到:

<root>
  <text1>120105728</text1>
  <perform_cnt>Buster</perform_cnt>
  <text1>120106394</text1>
  <perform_cnt>Buster</perform_cnt>
</root>

120105728
巴斯特
120106394
巴斯特

请使用您需要处理的输入文件的样本更新您的问题。请使用您需要处理的输入文件的样本更新您的问题。如果您想从输出中删除
,请将
/$1$3/
替换为
/
打印“$\code>的输出也不是最好的。使用
打印
@loldop-如果您正在寻找短代码,那么可能。否则我看不出有什么原因。然后,短代码可以看起来像
s/()(.*?)/$1$3/g&&print for替换整个
foreach
循环。它是相同的。如果需要,请使用
print;打印“\n”
打印“$\un”但我使用的是普通的
say
函数
say{return(@,“\n”)}
@loldop-我知道那是什么,但它不是标准用法,实际上
say
是来自Perl 5.10+的,我相信,所以不是每个Perl都能得到它。如果你想从输出中消除
,然后将代码
/$1$3/
中的内容替换为
/
。此外,
打印“$”的输出不是最好的。使用
打印
@loldop-如果您正在寻找短代码,那么可能。否则我看不出有什么原因。然后,短代码可以看起来像
s/()(.*?)/$1$3/g&&print for替换整个
foreach
循环。它是相同的。如果需要,请使用
print;打印“\n”
打印“$\un”但我使用的是普通的
say
函数
say{return(@,“\n”)}
@loldop-我知道那是什么,但它只是不是标准用法,实际上
say
是来自Perl 5.10+的,我相信,所以不是每个Perl都能得到它。