2015-05-03 40 views
1

我有一個來自iSpring Solutions的測驗系統,它將xml結果數據發送到我的php腳本。我想提取信息來更新一個mysql數據庫。當子節點名稱未知時提取xml數據

每個測驗都有不同的問題,所以xml文件並不總是相同的長度/結構,但是我需要獲取特定的數據。

這裏是從XML文件

<questions> 

    <matchingQuestion id="{3F033905-566A-4B67-B7E4-324E4367FD16}" status="correct" maxPoints="2" maxAttempts="1" awardedPoints="2" usedAttempts="1"> 
     <direction>Match the items on the left with the items on the right</direction> 
    </matchingQuestion> 

    <fillInTheBlankQuestionEx id="{6FCC5DAC-168F-4F44-87DF-9B1D162268D1}" status="partially" maxPoints="8" maxAttempts="1" awardedPoints="4" usedAttempts="1"> 
     <direction>Which indefinite article should be written in front of the following words - 'a' or 'an'?</direction> 
    </fillInTheBlankQuestionEx> 

    </questions> 

我需要 (一)獲取每個子節點的名稱/ ID提取 (B)得到的ID,狀態,maxPoints值 (C )然後得到'方向'值

節點名稱'matchingQuestion'或'fillInTheBlankQuestionEx'總是根據測驗改變順序。

我不知道如何獲取這些值,因爲每個測驗的問題類型都不相同!

提前感謝

約翰

完整的XML文件是:

<quizReport xsi:schemaLocation="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults quizReport.xsd" version="1" xmlns="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <quizSettings quizType="graded" maxScore="10" maxNormalizedScore="100" timeLimit="180"> 
    <passingPercent>0.8</passingPercent> 
    </quizSettings> 
    <summary score="2" percent="0.2" time="6"> 
    <variables> 
     <variable name="VARIABLE_2" title="DFE number (7 digit)" value="2112377"/> 
     <variable name="USER_NAME" title="Your name" value="John"/> 
     <variable name="VARIABLE_1" title="Your class" value="6M"/> 
    </variables> 
    </summary> 
    <questions> 
    <matchingQuestion id="{3F033905-566A-4B67-B7E4-324E4367FD16}" status="correct" maxPoints="2" maxAttempts="1" awardedPoints="2" usedAttempts="1"> 
     <direction>Match the items on the left with the items on the right</direction> 
     <premises> 
     <premise>an</premise> 
     <premise>a</premise> 
     </premises> 
     <responses> 
     <response>elephant</response> 
     <response>house</response> 
     </responses> 
     <matches> 
     <match premiseIndex="0" responseIndex="0"/> 
     <match premiseIndex="1" responseIndex="1"/> 
     </matches> 
     <userAnswer> 
     <match premiseIndex="0" responseIndex="0"/> 
     <match premiseIndex="1" responseIndex="1"/> 
     </userAnswer> 
    </matchingQuestion> 
    <fillInTheBlankQuestionEx id="{6FCC5DAC-168F-4F44-87DF-9B1D162268D1}" status="incorrect" maxPoints="8" maxAttempts="1" awardedPoints="0" usedAttempts="1"> 
     <direction>Which indefinite article should be written in front of the following words - 'a' or 'an'?</direction> 
     <details> 
     <blank userAnswer="" correct="false"> 
      <answer>a</answer> 
      <answer>a</answer> 
     </blank> 
     <text><![CDATA[ television 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>a</answer> 
     </blank> 
     <text><![CDATA[ boy 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>an</answer> 
     </blank> 
     <text><![CDATA[ umbrella 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>a</answer> 
     </blank> 
     <text><![CDATA[ house 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>an</answer> 
     </blank> 
     <text><![CDATA[ hour 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>an</answer> 
     </blank> 
     <text><![CDATA[ igloo 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>a</answer> 
     </blank> 
     <text><![CDATA[ minute 
]]></text> 
     <blank userAnswer="" correct="false"> 
      <answer>an</answer> 
     </blank> 
     <text><![CDATA[ elephant]]></text> 
     </details> 
    </fillInTheBlankQuestionEx> 
    </questions> 
</quizReport> 
+2

請編輯您的問題,並提供您作爲基礎開始的代碼。 – michi

回答

0

要開始在作弄我,我展示一個例子。假設,$文本包含XML

$xml = new SimpleXMLElement($text); 
echo '<pre>'; 
foreach ($xml->questions->children() as $q) { 
//  print_r($q); echo '<br/>'; 
     print_r((string)$q->getName());echo '<br/>'; 
     print_r((string)$q->attributes()->id);echo '<br/>'; 
     print_r((string)$q->direction);echo '<br/>'; 
} 

結果:

matchingQuestion 
{3F033905-566A-4B67-B7E4-324E4367FD16} 
Match the items on the left with the items on the right 
fillInTheBlankQuestionEx 
{6FCC5DAC-168F-4F44-87DF-9B1D162268D1} 
Which indefinite article should be written in front of the following words - 'a' or 'an'? 
0

謝謝您的回覆,我用下面的代碼,從Splash58重新寫正在更新MySQL數據庫的PHP:

foreach ($xml->questions->children() as $q) { 
$QuestionType=(string)$q->getName(); 
$QuestionUUID=(string)$q->attributes()->id; 
$QuestionAsked=(string)$q->direction; 

$query="INSERT INTO questions (UUID, Qtype, Question) VALUES ('".$QuestionUUID."', '".$QuestionType."', '".$QuestionAsked."')"; 
$mysqli->query($query); 

} 

但是,只有第一個問題被添加到數據庫。

+0

我已經嘗試了自己:)) - 在第二種情況下$ QuestionAsked包含引號,你應該使用mysql_real_escape_string() – splash58

+0

萬歲!像夢一樣工作!非常感謝你! –

相關問題