2015-12-29 38 views
-2

嗨PHP更新節點值,我需要使用PHP從exsiting XML內容 現有的XML其中有屬性

<?xml version="1.0" encoding="utf-8"?> 
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" BookId="10010"> 
    <STRING id="Name" >Test1</STRING> 
    <STRING id="ISBN">102399</STRING> 
</Book> 

新的XML修改XML

<?xml version="1.0" encoding="utf-8"?> 
<Book BookId="10010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > 
    <STRING id="Name" >XYZ</STRING> 
    <STRING id="ISBN">7777</STRING> 
</Book> 

注:的BookID應該是地方xmlns屬性前

預先感謝您。

+0

它不應該不管在什麼樣的順序屬性? – adeneo

+0

Stackoverflow可以幫助您修復代碼並幫助您解決問題,而不是爲您解決問題。你嘗試過什麼嗎? – Technotronic

+0

您是否檢查過PHP中的** DOMDocument **? –

回答

0

使用SimpleXML

$xml = simplexml_load_file($yourXMLPath); 

//Loop for element... 
foreach ($xml->STRING as $string) { 
    //Update value for <STRING id="Name">... 
    if($string->attributes()->id == 'Name'){ 
     $xml->STRING = 'XYZ'; 
    } 

    //Update value for <STRING id="Name">... 
    if($string->attributes()->id == 'ISBN'){ 
     $xml->STRING = '7777'; 
    } 
} 

// save the updated document 
$xml->asXML('newXML.xml'); 
+0

感謝分享我已經過驗證的代碼,但沒有爲我工作 – Prakash

+0

任何錯誤信息? –

+0

代碼中沒有錯誤,但沒有按預期更新值,代碼應該處理另外一件事(BookID應放在xmlns屬性之前) – Prakash

相關問題