douna2917 2016-09-23 18:39
浏览 59

如何使用PHP根据另一个节点值在xml中获取特定节点?

I am trying to work with XML like this sample:

<?xml version="1.0"?>
<quizReport version="1" xsi:schemaLocation="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults quizReport.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults">
<quizSettings timeLimit="0" maxNormalizedScore="100" maxScore="20" quizType="graded">

<passingPercent>0.6</passingPercent>
</quizSettings>
<summary time="4" percent="0.5" score="10">
<variables>
  <variable title="Nom" value="test1411" name="USER_NAME"/>

  <variable title="Courriel" value="" name="USER_EMAIL"/>
</variables>
</summary>


<questions>
<trueFalseQuestion usedAttempts="1" awardedPoints="10" maxAttempts="1" maxPoints="10" status="correct" id="{11AD6662-ACF3-4C2B-A3E6-04311C0DD8DB}">
<direction>Le mot de passe de votre compte iNews est indépendant de votre mot de passe radiocanadien géré par ActiveDirectory. </direction>
<answers userAnswerIndex="0" correctAnswerIndex="0">
<answer>Vrai </answer>
<answer>Faux </answer>
</answers>
</trueFalseQuestion>

<trueFalseQuestion usedAttempts="1" awardedPoints="0" maxAttempts="1" maxPoints="10" status="incorrect" id="{F13CF7F1-C830-41AF-A54C-CE78EE383611}">
<direction>Le protocole FTP permet de reprendre un transfert de fichier qui a été arrêté, sans recommencer le transfert depuis le début. </direction>
<answers userAnswerIndex="1" correctAnswerIndex="0">
<answer>Vrai </answer>
<answer>Faux </answer>
</answers> 
</trueFalseQuestion>
</questions> 
</quizReport>

Each question has an ID attribute, e.g. {11AD6662-ACF3-4C2B-A3E6-04311C0DD8DB}. The status of the question is returned as text (i.e. correct or incorrect).

My goal is to extract each question status and put in a database where a column is labelled with the same question ID (I'm comfortable with MySQL INSERT).

I've tried many solutions (xPath, foreach...) and I can't get any piece of code to work. All my efforts were made with SimpleXMLElement.

So, at the end, I will insert the value correct or incorrect into the DB under a column labelled with question id (e.g. {11AD6662-ACF3-4C2B-A3E6-04311C0DD8DB}) for every single question of the XML (which can contain up to 100 questions). So some sort of loop through the XML is needed.

I must point out that the tag name truFalseQuestion may change according to the type of question (another question could be multipleChoiceQuestion for instance). I made this simple XML to help me understand on a step by step basis.

  • 写回答

2条回答 默认 最新

  • dsjq6977 2016-09-23 19:05
    关注

    You can use the PHP SimpleXML library to handle parsing the xml for you. e.g.:

    //presuming that xml data is stored in $xmlContents
    $xmlData = new SimpleXMLElement($xmlContents);
    $keys = get_object_vars($xmlData);
    $aValues = array();
    foreach($keys as $key=>$property) {
        if (stripos($key,'questions')!== false) {       
            foreach($property as $questionType=>$question) {
                $aValues[] = "('".(string)$question['id']."','".(string)$question['status']."')";
            }
        }
    }
    if (count($aValues)) {
        //presumed table and columns - modify per your names - this is a sample SQL statement - if you are using a different database variant, modify accordingly
        $query = 'INSERT INTO questionStatus (questionId,status) VALUES ' .     implode(',',$aValues);
        print($query);
        //@TODO: run insert statement with your database layer code
    }
    

    Output with your data:

    INSERT INTO questionStatus (questionId,status) VALUES ('{11AD6662-ACF3-4C2B-A3E6-04311C0DD8DB}','correct'),('{F13CF7F1-C830-41AF-A54C-CE78EE383611}','incorrect')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)