2014-12-03 102 views
2

我有以下XML文件更新XML文件(SimpleXMLElement對象)使用的字符串

<?xml version="1.0" encoding="UTF-8"?> 
<jmeterTestPlan version="1.2" properties="2.6" jmeter="2.11 r1554548"> 
<hashTree> 
<TestPlan ...> 
    <elementProp name="TestPlan.user_defined_variables" ...> 
    <collectionProp name="Arguments.arguments"> 

     <elementProp name="nb.publisher_Path" elementType="Argument"> 
     <stringProp name="Argument.name">nb.publisher_Path</stringProp> 
     <stringProp name="Argument.value">http://mmm.com</stringProp> 
     <stringProp name="Argument.metadata">=</stringProp> 
     </elementProp> .... 

,我想裏面更新網址:elementProp名= 「nb.publisher_Path」

來自:http://mmm.com 於:http://aaa.com

,所以我已經寫了下面的PHP代碼:

<?php 
$xml = new SimpleXmlElement(file_get_contents("C://...Testing_User.jmx")); 

    $result_publisher_Path_2 = $xml->xpath('//elementProp[@name="nb.publisher_Path"]/stringProp[@name="Argument.value"]')[0]; 

    $result_advertiser_Path = $xml->xpath('//elementProp[@name="nb.advertiser_Path"]/stringProp[2]'); 
    $result_adbrooker_Path = $xml->xpath('//elementProp[@name="nb.adbrooker_Path"]/stringProp[2]'); 
    echo ($result_publisher_Path_2); 
    (string)$xml->xpath('//elementProp[@name="nb.publisher_Path"]/stringProp[@name="Argument.value"]')[0] = "http://aaa.com"; 
    echo $xml->asXml(); 

所以

echo ($result_publisher_Path_2); 

的結果是,我想更換正確的網址,當我試圖與

(string)$xml->xpath('//elementProp[@name="nb.publisher_Path"]/stringProp[@name="Argument.value"]')[0] = "http://aaa.com"; 

改變它,它沒有被改變。

任何想法?

+0

請簡化您的XML減少到最低限度。 – michi 2014-12-03 12:44:15

回答

1

嘗試使用這樣的:

$result_publisher_Path_2[0] = "http://aaa.com"; 
+2

第二個例子是要走的路@sanki – michi 2014-12-03 12:43:25

+0

@sanki:謝謝sanki,第二個是對的(Y) – AHS 2014-12-03 13:20:20