我有这个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>user_0002@atyaf.me</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>
我需要替换某些字段的值(UserID,firstName,password)并输出要保存的同名文件。
使用下面的代码我将更改xml字段的语法(xml格式受到干扰):
XMLout( $xml, KeepRoot => 1, NoAttr => 1, OutputFile => $xml_file, );
请问您如何编辑xml文件而不更改其语法?
答案 0 :(得分:0)
您可以为perl签出XML :: Simple解析器。您可以参考CPAN站点。我用它来解析XML文件,但我认为这也应该允许修改。
答案 1 :(得分:-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;