1

我有一个来自 iSpring Solutions 的测验系统,它将 xml 结果数据发送到我的 php 脚本。我想提取信息以更新 mysql 数据库。

每个测验都有不同的问题,因此 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>

我需要 (a) 获取每个子节点的名称/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>
4

2 回答 2

0

为了开始你的编码,我举了一个例子。假设,$text 包含 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'? 
于 2015-05-03T13:38:37.040 回答
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);

}

但是,只有第一个问题被添加到数据库中。

于 2015-05-03T14:25:54.300 回答