gpt4 book ai didi

php - 子节点名称未知时提取xml数据

转载 作者:行者123 更新时间:2023-11-30 22:44:34 24 4
gpt4 key购买 nike

我有一个来自 iSpring Solutions 的测验系统,它将 xml 结果数据发送到我的 php 脚本。我想提取信息来更新 mysql 数据库。

每个测验都有不同的问题,因此 xml 文件的长度/结构并不总是相同,但我需要获取特定数据。

这是xml文件的摘录

<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) 获取每个子节点的名称/id(b) 获取 id、status、maxPoints 值(c) 然后获取 'direction' 值

节点名称“matchingQuestion”或“fillInTheBlankQuestionEx”总是根据测验更改顺序。

我不知道如何获取这些值,因为每个测验的问题类型各不相同!

提前致谢

约翰

完整的 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>

最佳答案

为了开始您的代码编写,我展示了一个示例。假设,$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/>';
}

结果:

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'?

关于php - 子节点名称未知时提取xml数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com