-1

simplexmlを使用してxmlのノード属性を更新することは可能ですか? 例: - 私のプログラムは、実際には教師用のクイズ エディターです。教師として、特定の質問属性を編集したいと考えています。すなわち

<Quiz>
<topic text="Preparation for Exam">
    <subtopic text="Math">
              <question text="1 + 1 = ?"> 
              <answer num="A" Text="1" /> 
              <answer num="B" Text="2" /> 
              <answer num="C" Text="3" /> 
              <answer num="D" Text="4" /> 
              </question>
              <question text="4 * 4 = ?" > 
              <answer num="A" Text="12" /> 
              <answer num="B" Text="14" /> 
              <answer num="C" Text="16" /> 
              <answer num="D" Text="18" /> 
              </question>
       </subtopic>
       </topic>
       </Quiz>

出来ますか?前のノードを削除してから編集したノードを挿入するなど、多くの方法を試しましたが。しかし、このトリックは最後のノードでのみ機能します...他のノードではスワッピングを行うだけです

4

1 に答える 1

0
<?php
$subtopic = new SimpleXMLELement(data());
$subtopic->question[1]['text'] = 'lalala';
$subtopic->question[1]['foo'] = 'bar';

foreach( $subtopic->question[1]->answer as $answer) {
    $answer['Text'] .= '(dez)';
}

echo $subtopic->asXML();


function data() {
    return <<< eox
<subtopic text="Math">
<question text="1 + 1 = ?"> 
<answer num="A" Text="1" /> 
<answer num="B" Text="2" /> 
<answer num="C" Text="3" /> 
<answer num="D" Text="4" /> 
</question>
<question text="4 * 4 = ?" > 
<answer num="A" Text="12" /> 
<answer num="B" Text="14" /> 
<answer num="C" Text="16" /> 
<answer num="D" Text="18" /> 
</question>
</subtopic>
eox;
}

版画

<?xml version="1.0"?>
<subtopic text="Math">
<question text="1 + 1 = ?"> 
<answer num="A" Text="1"/> 
<answer num="B" Text="2"/> 
<answer num="C" Text="3"/> 
<answer num="D" Text="4"/> 
</question>
<question text="lalala" foo="bar"> 
<answer num="A" Text="12(dez)"/> 
<answer num="B" Text="14(dez)"/> 
<answer num="C" Text="16(dez)"/> 
<answer num="D" Text="18(dez)"/> 
</question>
</subtopic>
于 2013-04-17T11:46:20.717 に答える