Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
基于另一个标记值更改PERL中XML标记的值_Xml_Perl - Fatal编程技术网

基于另一个标记值更改PERL中XML标记的值

基于另一个标记值更改PERL中XML标记的值,xml,perl,Xml,Perl,情况就是这样 <Student_Detail> <Name> Alexander Stephen </Name> <Employee_id> 234567 </Employee_id> </Student_Detail> <Student_Detail> <Name> Geet Sethi </Name> <Employee_id> 264577 </Employee_

情况就是这样

<Student_Detail>
<Name> Alexander Stephen </Name>
<Employee_id> 234567 </Employee_id>
</Student_Detail>

<Student_Detail>
<Name> Geet Sethi </Name>
<Employee_id> 264577 </Employee_id>
</Student_Detail>   

亚历山大·斯蒂芬
234567
吉特塞蒂
264577
考虑到以上内容是XML文件的一部分。我的工作是根据姓名更改员工ID

比如说Alexander Stephen,新员工ID应该是264785

我需要通过PERL进行上述更改。但问题是,我对它比较陌生


我的尝试是识别标记,将其存储在数组中,然后根据名称标记发布新的员工ID。有人能帮我提出一个启动想法,以便我更好地加快代码的编写。

使用XML::Twig的解决方案:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

# a hash name => new_id
my %new_id= ( 'Alexander Stephen' => '264785'
            );

my $t= XML::Twig->new( twig_handlers => { Student_Detail => \&student },
                       keep_spaces => 1,
                     )
                 ->parse( \*DATA); # or parsefile( 'my_file') in the real code

# called any time a Student_Detail element has been completely parsed
sub student
  { my( $t, $student)= @_; 

    # use trimmed_field to get rid of extra spaces
    my $name= $student->trimmed_field( 'Name'); 

    if( my $new_id= $new_id{$name}) 
      { $student->first_child( 'Employee_id')->set_text( $new_id); }

    $t->flush; # outputs the XML so far
  }

__DATA__
<Students>
<Student_Detail>
<Name> Alexander Stephen </Name>
<Employee_id> 234567 </Employee_id>
</Student_Detail>

<Student_Detail>
<Name> Geet Sethi </Name>
<Employee_id> 264577 </Employee_id>
</Student_Detail>   
</Students>
#/usr/bin/perl
严格使用;
使用警告;
使用XML::Twig;
#哈希名称=>new\u id
我的%new_id=('Alexander Stephen'=>'264785'
);
my$t=XML::Twig->new(Twig\u handlers=>{Student\u Detail=>\&Student},
保持_空格=>1,
)
->解析(\*数据);#或实际代码中的parsefile('my_file')
#在完全解析Student_细节元素时调用
学生
{我的($t,$student)=@;
#使用修剪的_字段来消除多余的空格
我的$name=$student->trimmed_字段('name');
if(my$new_id=$new_id{$name})
{$student->first_child('Employee_id')->set_text($new_id);}
$t->flush;#输出到目前为止的XML
}
__资料__
亚历山大·斯蒂芬
234567
吉特塞蒂
264577
这会将输出打印到标准输出。如果要更新文件,可以将其重定向到特定文件,或者使用
parsefile\u inplace
。。。到位


您的XML有点奇怪,名称和ID周围的空格可能会产生问题。你确定它们存在于真实数据中吗?

谢谢:)让我检查一下这个XML表示不精确。我刚刚为这个论坛创建了一个实际需求的副本。但再次感谢你的帮助(@mirod),同时我似乎又犯了一个错误。当我尝试使用库函数(使用XML::LibXML或XML::Twig)时,我遇到了一个错误:@INC(@INC-contains:C:/Perl/site/lib C:/Perl/lib.)中找不到XML/LibXML.pm,它被困在这里并进行研究。请提供帮助。您需要使用CPAN、cpanm、ppm或系统软件包管理器安装您使用的模块。