2013-10-10 102 views
0

我需要在關閉節點之前添加我的XML樹,我似乎無法找到解決方案。紅色塊中的示例僅需要移動到</quiz>節點之上。在關閉節點之前添加一個XML節點

enter image description here

這在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'); 
+0

_「樣品quiz.xml下面的紅色塊只需要移動到''n ode。「_ - 沒有像」closing _node_「這樣的東西,只有一個結束_tag_。如果你想追加一個元素作爲現有節點的最後一個子元素,那麼使用appendChild。 – CBroe

回答

0

對於一個給定的輸入quiz.xml有適合您的代碼

<?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'); 

以下的PHP的

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

內容產生想要的結果

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

非常感謝!這是我需要的。 – mrAfzaL

0
在上面的代碼

要裝入一個XML和簡單添加節點,這將總是在文件的結尾加入。

嘗試使用附加,而不是增加對前:

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

僅供參考。