2012-04-15 22 views
0

タグの内容を変更するにはどうすればよいですか(属性ではなくコンテンツのみ)。私はこの親タグの内側のタグは必要ありません。私はちょうどそれの中にいくつかのテキストを入れる必要があります。次のXMLファイル( "1. Hello Worldのを" 交換):あなたは両親の構造にあなたを知っていればSimpleXMLElementタグコンテンツ(値)を変更する方法

<config name="THIS" type="string">1. Hello World)</config> 
<config name="IS" type="string">2. Hello World!</config> 
<config name="SPARTA" type="string">3. Hello World?</config> 

// create simple xml object 
$xml = new SimpleXMLElement(file_get_contents('file.xml')); 

// new names and values 
$names = array('THIS', 'SPARTA'); 
$values = array('1. Good bye world(', '3. Good bye world?'); 

// replace here 
foreach($xml as $a => $xmlElement) { 
    $indexOfName = array_search($names, (string)$xmlElement['name']); 
    if ($indexOfName !== false) { 
      // here I need to replace the value (e.g. 1. Hello World)) of config 
      // with the attribute @name == $names[$i] with the new value 
      // $values[$i] (3. Good bye world?) 
    } 
} 

答えて

1

一時的な解決策が見つかりました。その新しい単純なxmlを作成し、コンテンツの値を置き換えて現在の単純なxmlからすべてのものをコピーします。

ただし、一時的です。誰かがより良い解決策を生み出せるかどうかを願っています。

1

通常は、親要素を必要としますか、 xpathを試すことができます。それ以外の場合、このハックを試してみるとよいでしょう:

$xmlElement->{0} = $values[$indexOfName]; 
+0

私はすでにこれを試しています。それは仕事をしなかった – pleerock

関連する問題