我需要在红色<title> DATA HERE </title>
块中插入数据
<description>
<text> DATA HERE</text>
</description>
使用Simple XML。以下是XML树的示例 -
以下是从<question>
开始创建XML标记和数据的PHP代码,但我不知道如何执行上述操作。
$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('test.xml');
答案 0 :(得分:0)
像这样:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<quizzes>
<quiz>
<title>
</title>
<description>
<text>
</text>
</description>
</quiz>
</quizzes>';
$quizzes = new SimpleXMLElement($xml);
$quizzes->quiz[0]->title = "TITLE";
$quizzes->quiz[0]->description->text = "DESCRIPTION";
echo $quizzes->asXML();
?>
输出:
<?xml version="1.0" encoding="UTF-8"?>
<quizzes>
<quiz>
<title>TITLE</title>
<description>
<text>DESCRIPTION</text>
</description>
</quiz>
</quizzes>