I have a XML file of the format:
我有一个格式的XML文件:
<outer1>
<inner1>
<name>Stonecold</name>
<profession>warrior</profession>
<org>wwf</org>
</inner1>
<inner1>
<name>Shanebond</name>
<profession>Bowler</profession>
<org>newzealand</org>
</inner1>
<inner1>
<name>brain schemidit</name>
<profession>Chairman</profession>
<org>Google</org>
</inner1>
</outer1>
I want to change the value of Shanebond
to Shane Bond
.
我想将Shanebond的价值改为Shane Bond。
I was using XML::Simple
, but the result was a hash.
我使用XML :: Simple,但结果是哈希。
I want the same format as the input file. E.g: the output file should be as follows:
我想要与输入文件相同的格式。例如:输出文件应如下所示:
<outer1>
<inner1>
<name>Stonecold</name>
<profession>warrior</profession>
<org>wwf</org>
</inner1>
<inner1>
<name>Shane Bond</name>
<profession>Bowler</profession>
<org>newzealand</org>
</inner1>
<inner1>
<name>brain schemidit</name>
<profession>Chairman</profession>
<org>Google</org>
</inner1>
</outer1>
Please advice how to do this.
请告知如何做到这一点。
Thanks in advance.
提前致谢。
I want the output file to be saved in the same directory and if possible with the same name. is it possible?
我希望输出文件保存在同一目录中,如果可能的话,使用相同的名称。可能吗?
4 个解决方案
#1
4
XML::Simple
has options that allow you to specify how input will be transformed into a Perl data structure and how that structure will be output:
XML :: Simple有一些选项,允许您指定如何将输入转换为Perl数据结构以及如何输出该结构:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
my $xml_file = 'b.xml';
my $xml = XMLin(
$xml_file,
KeepRoot => 1,
ForceArray => 1,
);
$xml->{outer1}->[0]->{inner1}->[1]->{name} = 'Shane Bond';
XMLout(
$xml,
KeepRoot => 1,
NoAttr => 1,
OutputFile => $xml_file,
);
XML::Simple
does get a little hairy if you do anything interesting because its purpose is not to be a general purpose XML library but to provide a simple way to deal with configuration files written in XML.
如果你做任何有趣的事情,XML :: Simple确实有点毛茸茸,因为它的目的不是成为通用的XML库,而是提供一种简单的方法来处理用XML编写的配置文件。
CPAN has a plethora of XML related modules. Unless this was a one-off issue you had to deal with, it would be worth looking into some of the more capable and better suited modules.
CPAN有许多与XML相关的模块。除非这是一个你必须处理的一次性问题,否则值得研究一些功能更强大,更适合的模块。
#2
8
When it comes to reading or manipulating a XML file then XML::Twig is often the first tool I look to use.
在阅读或操作XML文件时,XML :: Twig通常是我希望使用的第一个工具。
At first I thought it maybe an overkill for your requirement but then I noticed it did come with a parsefile_inplace() option:
起初我认为它可能对你的要求有点过分,但后来我发现它确实附带了一个parsefile_inplace()选项:
use strict;
use warnings;
use XML::Twig;
XML::Twig->new(
pretty_print => 'indented',
twig_handlers => {
name => sub {
$_->set_text( 'Shane Bond' )->flush if $_->text eq 'Shanebond'
},
},
)->parsefile_inplace( 'data.xml', 'bak_*' );
NB. If you don't want to keep a backup file then remove the second arg ('bak_*').
NB。如果您不想保留备份文件,请删除第二个arg('bak_ *')。
#3
4
Why bother processing it as XML at all? Why not just do a regexp-replace?
为什么还要把它当作XML来处理呢?为什么不做一个regexp替换?
perl -pi -e 's/Shanebond/Shane Bond/' filename.xml
That will do the replacement in place, keeping the same filename and everything.
这将替换到位,保持相同的文件名和一切。
#4
3
Have you tried XMLout with OutputFile
你有没有尝试使用OutputFile的XMLout
From the documentation for XML::Simple:
从XML :: Simple的文档:
The default behaviour of XMLout() is to return the XML as a string. If you wish to write the XML to a file, simply supply the filename using the 'OutputFile' option.
This option also accepts an IO handle object - especially useful in Perl 5.8.0 and later for output using an encoding other than UTF-8, eg:
XMLout()的默认行为是将XML作为字符串返回。如果您希望将XML写入文件,只需使用“OutputFile”选项提供文件名。此选项还接受IO句柄对象 - 在Perl 5.8.0及更高版本中使用UTF-8以外的编码进行输出时尤其有用,例如:
open my $fh, '>:encoding(iso-8859-1)', $path or die "open($path): $!";
XMLout($ref, OutputFile => $fh);
#1
4
XML::Simple
has options that allow you to specify how input will be transformed into a Perl data structure and how that structure will be output:
XML :: Simple有一些选项,允许您指定如何将输入转换为Perl数据结构以及如何输出该结构:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
my $xml_file = 'b.xml';
my $xml = XMLin(
$xml_file,
KeepRoot => 1,
ForceArray => 1,
);
$xml->{outer1}->[0]->{inner1}->[1]->{name} = 'Shane Bond';
XMLout(
$xml,
KeepRoot => 1,
NoAttr => 1,
OutputFile => $xml_file,
);
XML::Simple
does get a little hairy if you do anything interesting because its purpose is not to be a general purpose XML library but to provide a simple way to deal with configuration files written in XML.
如果你做任何有趣的事情,XML :: Simple确实有点毛茸茸,因为它的目的不是成为通用的XML库,而是提供一种简单的方法来处理用XML编写的配置文件。
CPAN has a plethora of XML related modules. Unless this was a one-off issue you had to deal with, it would be worth looking into some of the more capable and better suited modules.
CPAN有许多与XML相关的模块。除非这是一个你必须处理的一次性问题,否则值得研究一些功能更强大,更适合的模块。
#2
8
When it comes to reading or manipulating a XML file then XML::Twig is often the first tool I look to use.
在阅读或操作XML文件时,XML :: Twig通常是我希望使用的第一个工具。
At first I thought it maybe an overkill for your requirement but then I noticed it did come with a parsefile_inplace() option:
起初我认为它可能对你的要求有点过分,但后来我发现它确实附带了一个parsefile_inplace()选项:
use strict;
use warnings;
use XML::Twig;
XML::Twig->new(
pretty_print => 'indented',
twig_handlers => {
name => sub {
$_->set_text( 'Shane Bond' )->flush if $_->text eq 'Shanebond'
},
},
)->parsefile_inplace( 'data.xml', 'bak_*' );
NB. If you don't want to keep a backup file then remove the second arg ('bak_*').
NB。如果您不想保留备份文件,请删除第二个arg('bak_ *')。
#3
4
Why bother processing it as XML at all? Why not just do a regexp-replace?
为什么还要把它当作XML来处理呢?为什么不做一个regexp替换?
perl -pi -e 's/Shanebond/Shane Bond/' filename.xml
That will do the replacement in place, keeping the same filename and everything.
这将替换到位,保持相同的文件名和一切。
#4
3
Have you tried XMLout with OutputFile
你有没有尝试使用OutputFile的XMLout
From the documentation for XML::Simple:
从XML :: Simple的文档:
The default behaviour of XMLout() is to return the XML as a string. If you wish to write the XML to a file, simply supply the filename using the 'OutputFile' option.
This option also accepts an IO handle object - especially useful in Perl 5.8.0 and later for output using an encoding other than UTF-8, eg:
XMLout()的默认行为是将XML作为字符串返回。如果您希望将XML写入文件,只需使用“OutputFile”选项提供文件名。此选项还接受IO句柄对象 - 在Perl 5.8.0及更高版本中使用UTF-8以外的编码进行输出时尤其有用,例如:
open my $fh, '>:encoding(iso-8859-1)', $path or die "open($path): $!";
XMLout($ref, OutputFile => $fh);