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.
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')
You could run through all child nodes of the questions element.
Each child node's tagname can be found using the getName() function.
Here's some sample code. I hope, it can help you to get an idea.
<?php
//if your XML is stored in a file named "test.xml", you can load it from there.
//Otherwise try simplexml_load_string($xmlstring)
$file=simplexml_load_file("test.xml");
//get the questions
$questions=$file->questions->children();
foreach($questions as $question) {
//as a sample there is some data printed for each question.
print "QuestionType: ". $question->getName()."
";
print "QuestionID: ". $question->attributes()->id."
";
print "Direction: ".(string) $question->direction."
";
print "
";
print "Answers:
";
foreach($question->answers->answer as $answer){
print (string)$answer."
";
}
print "
";
}
[EDIT]
It prints something like this:
QuestionType: trueFalseQuestion QuestionID: {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. Answers: Vrai Faux QuestionType: trueFalseQuestion QuestionID: {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. Answers: Vrai Faux
[/EDIT]
Edit2: Added the id to the sample code and sample output