2011-12-30 111 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文件,但我認爲這也應該允許修改。