恢复属性标记' selected = true'来自XML组

时间:2016-05-13 18:08:30

标签: xml

我再次请求您对XML组和标签的帮助。我将非常感谢您提供的任何意见。提前谢谢。

所以,我有这个XML小组:

<multipleResponseQuestion status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1">
<direction>Pick the correct option(s):</direction>
  <answers>
    <answer correct="true" selected="true">option 1</answer>
    <answer correct="false" selected="false">option 2</answer>
    <answer correct="false" selected="false">option 3</answer>
    <answer correct="false" selected="false">option 4</answer>
  </answers>
</multipleResponseQuestion>

我想在数据库中写下用户选择的选项或选项(多项选择)。

我写了这段代码:

if($answer_group->getName()=="multipleResponseQuestion") { //MULTIPLE RESPONSE QUESTION

$multipleResponseQuestionNumber = 1;

foreach($answer_group->answers->answer as $userMultipleResponseQuestion){

if($status!="notAnswered" && isset($userMultipleResponseQuestion->attributes()->answer)) {
$userAnswer = (string)$userMultipleResponseQuestion->attributes()->selected;
    $values .= "\"" . $userAnswer . "\", ";
    } else {
    $values .= "\"" . $status_notAnswered . "\", ";
}

$columns_create .= $prefix . $questionNumber . "_" . $multipleResponseQuestionNumber . " VARCHAR(50), ";
$columns_insert .= $prefix . $questionNumber . "_" . $multipleResponseQuestionNumber . ", ";

$multipleResponseQuestionNumber++;
}

所以,我需要恢复&#34; selected = true&#34;将它们作为$ userAnswer写入数据库的选项。但我无法弄清楚如何调用此属性。有人可以帮忙吗?

最佳

JPMD

1 个答案:

答案 0 :(得分:0)

毕竟这很简单:

if($status != "notAnswered"
  && $userMultipleResponseQuestion->attributes()->selected == 'true')
{
    $userAnswer = (string)$userMultipleResponseQuestion;
}