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>