dongli7236 2016-05-10 22:55
浏览 14
已采纳

如何从XML标记中恢复项目

I am stuck with getting the value from an XML element list. I wonder if someone here could help?

Here is the problem. I have this XML element:

<sequenceQuestion status="correct" maxPoints="10" maxAttempts="1" awardedPoints="10" usedAttempts="1">
  <direction>Order the following options</direction>
  <answers>
    <answer userDefinedPosition="0">Option 1</answer>
    <answer userDefinedPosition="1">Option 2</answer>
    <answer userDefinedPosition="2">Option 3</answer>
    <answer userDefinedPosition="3">Option 4</answer>
    <answer userDefinedPosition="4">Option 5</answer>
  </answers>
</sequenceQuestion>

I need to print the elements in the tag. For example, I need to print "Option 1", "Option 2", ... , "Option 5" in a webpage by the order they were selected by the user.

If I use this code:

$SequenceQuestionNumber = 1;

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

if($status!="notAnswered" && isset($userSequenceQuestion->attributes()->userDefinedPosition)) {
    $userDefinedPosition = (string)$userSequenceQuestion->attributes()->userDefinedPosition;
    $values .= "\"" . $userDefinedPosition . "\", ";

I get as answer values "0", "1", "2", "3" and "4". But what I need is not the 'userDefinedPosition" but rather the text associated with it. For "0" I need to print "Option 1", for "1" I need "Option 2", for "3" I need "Option 2", etc...

I have tried to build an array like this:

$answers = (array)$userSequenceQuestion->answer;
$values .= "\"" . $answers[$userDefinedPosition] . "\", ";

but I just get blanks (nothing) printed.

I guess what is messing my reference is the userDefinedPosition inside the tag, but I am not sure on how to get the labels instead of the index numbers.

  • 写回答

2条回答 默认 最新

  • duanci9305 2016-05-11 21:31
    关注

    Ok. So I got it. Thank you all that contributed to an answer. This is the code which writes the user answer ordering:

    if($answer_group->getName()=="sequenceQuestion") { //SEQUENCE QUESTION
    
    $SequenceQuestionNumber = 1;
    
    foreach($answer_group->answers->answer as $userSequenceQuestion){
    
    if($status!="notAnswered" && isset($userSequenceQuestion->attributes()->userDefinedPosition)) {
    $userDefinedPosition = (int)$userSequenceQuestion->attributes()->userDefinedPosition;
    $answers = (string)$answer_group->answers->answer[$userDefinedPosition];
    $values .= "\"" . $answers . "\", ";
    } else {
    $values .= "\"" . $status_notAnswered . "\", ";
    }
    
    $SequenceQuestionNumber++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)