在所选标签中插入XML数据

时间:2013-10-10 13:37:34

标签: php xml simplexml

我需要在红色<title> DATA HERE </title>块中插入数据 <description> <text> DATA HERE</text> </description> 使用Simple XML。以下是XML树的示例 - enter image description here

以下是从<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');

1 个答案:

答案 0 :(得分:0)

像这样:

http://3v4l.org/8F5eQ

<?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>