quiz.xml
の内容を持つ特定の入力に対して
<?xml version="1.0"?>
<quizzes>
<quiz>
<question>
<text>Choose the correct word for hot + est</text>
<option>
<text>hottest</text>
<score>5</score>
<explanation>
<text>Correct!</text>
</explanation>
</option>
<option>
<text>hotestt</text>
<score>0</score>
<explanation>
<text>Incorrect!</text>
</explanation>
</option>
<option>
<text>hotest</text>
<score>0</score>
<explanation>
<text>Incorrect!</text>
</explanation>
</option>
</question>
</quiz>
</quizzes>
あなたのコードから適応された次のPHP
<?php
$que = 'fsa';
$answer1 = 'cs';
$score1 = 5;
$explanation1 = 'hgfhfg';
$xml = simplexml_load_file('quiz.xml');
$questionLoad = $xml->children()[0]->addChild('question');
$textQue = $questionLoad->addChild('text', $que);
$optionNode = $questionLoad->addChild('option');
$ans1 = $optionNode->addChild('text', $answer1);
$score = $optionNode->addChild('score', $score1);
$explain = $optionNode->addChild('explanation');
$expl1 = $explain->addChild('text', $explanation1);
$xml->asXML('quiz.xml');
の望ましい結果quiz.xml
を生成します
<?xml version="1.0"?>
<quizzes>
<quiz>
<question>
<text>Choose the correct word for hot + est</text>
<option>
<text>hottest</text>
<score>5</score>
<explanation>
<text>Correct!</text>
</explanation>
</option>
<option>
<text>hotestt</text>
<score>0</score>
<explanation>
<text>Incorrect!</text>
</explanation>
</option>
<option>
<text>hotest</text>
<score>0</score>
<explanation>
<text>Incorrect!</text>
</explanation>
</option>
</question>
<question><text>fsa</text><option><text>cs</text><score>5</score><explanation><text>hgfhfg</text></explanation></option></question></quiz>
</quizzes>