繁体   English   中英

在关闭节点之前添加XML节点

[英]Add an XML node before a closing node

我需要在关闭节点之前添加XML树,但似乎找不到解决方案。下面红色方框中的示例仅需移至</quiz>节点上方。

在此处输入图片说明

这是我用PHP完成的XML代码,在红色块中创建了XML的上述部分。

$xml = simplexml_load_file('quiz.xml');


$questionLoad = $xml->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');

在上面的代码中,您正在加载xml并简单地添加一个节点,该节点将始终添加在文件末尾。

尝试使用append代替ex的add:

$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();

仅供参考。

对于给定的输入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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM