2011-12-30 45 views
0

我有这个XML文件:perl脚本来代替XML值

<?xml version="1.0" encoding="ISO-8859-1"?> 
<BroadsoftDocument protocol = "OCI" xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <sessionId xmlns="">169.254.52.85,16602326,1324821125562</sessionId> 
    <command xsi:type="UserAddRequest14sp9" xmlns=""> 
     <serviceProviderId>AtyafBahrain</serviceProviderId> 
     <groupId>LoadTest</groupId> 
     <userId>[email protected]</userId> 
     <lastName>0002</lastName> 
     <firstName>user</firstName> 
     <callingLineIdLastName>0002</callingLineIdLastName> 
     <callingLineIdFirstName>user</callingLineIdFirstName> 
     <password>123456</password> 
     <language>English</language> 
     <timeZone>Asia/Bahrain</timeZone> 
     <address/> 
    </command> 
</BroadsoftDocument> 

,我需要更换一些领域(用户名,名字,密码)和输出文件被保存在相同的价值观名称。

使用下面的代码我将改变XML字段的语法(XML格式受到干扰):

XMLout( $xml,  KeepRoot => 1,  NoAttr => 1,  OutputFile => $xml_file,); 

可以请你指点如何编辑XML文件,而无需改变其语法?

回答

-1
# open XML file (input the XML file name) 
open (INPUTFILE, "+<$filename_1");  
@file = <INPUTFILE>; 
seek INPUTFILE,0,0; 
foreach $file (@file) 
{ 
    # Find string_1 and replace it by string_2 
    $file =~ s/$str_1/$str_2/g; 

    # write to file 
    print INPUTFILE $file; 
} 
close INPUTFILE; 
+0

欢迎来到SO!尽管在XML上进行正则表达式替换并不是一个好主意。使用一些'<'字符来使整个束无效是很容易的。此外,您的代码不会在[strict](http://p3rl.org/strict)ures下编译,今天优先使用“open”的三参数形式。 – memowe 2012-10-26 09:35:57

0

您可以检出XML :: Simple parser for perl。你可以参考CPAN网站。我用它来解析XML文件,但我认为这也应该允许修改。