Php 子节点名称未知时提取xml数据
我有一个来自iSpring Solutions的测验系统,它将xml结果数据发送到我的php脚本。我想提取信息来更新mysql数据库 每个测验都有不同的问题,因此xml文件的长度/结构并不总是相同的,但是我需要获得特定的数据 下面是xml文件的摘录Php 子节点名称未知时提取xml数据,php,mysql,xml,child-nodes,Php,Mysql,Xml,Child Nodes,我有一个来自iSpring Solutions的测验系统,它将xml结果数据发送到我的php脚本。我想提取信息来更新mysql数据库 每个测验都有不同的问题,因此xml文件的长度/结构并不总是相同的,但是我需要获得特定的数据 下面是xml文件的摘录 <questions> <matchingQuestion id="{3F033905-566A-4B67-B7E4-324E4367FD16}" status="correct" maxPoints="2" maxAtt
<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”或“an”前面?
我需要
(a) 获取每个子节点的名称/id
(b) 获取id、状态、maxPoints值
(c) 然后得到“方向”值
节点名称“matchingQuestion”或“FillingBlankQuestionEx”总是根据测验的不同而改变顺序
我不知道如何获得这些值,因为每个测验的问题类型都不同
非常感谢
约翰
完整的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>
0.8
将左侧的项目与右侧的项目匹配
一
A.
大象
房子
哪一个不定冠词应该写在下面的单词“a”或“an”前面?
A.
A.
A.
一
A.
一
一
A.
一
要开始编码,我举一个例子。假设,$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/>';
}
感谢您的回复,我使用了下面的代码,从Splash58重新编写了更新mysql数据库的php:
但是,数据库中只添加了第一个问题。请编辑您的问题,并提供您作为起点的代码。我自己尝试过:)-在第二种情况下,$QuestionAsked包含引号,您应该使用mysql\u real\u escape\u string()好哇!像做梦一样工作!非常感谢你!
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'?
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);
}