0
請問如何讀取和解析包含屬性的XML文件。解析具有屬性的XML文件
XML文件是這樣的,有很多「問題」節點
<questions>
<description>
Here goes description....
</description>
<evaluation_order>
1
</evaluation_order>
<question text="question1">
<answer score="10">answer 1</answer>
<answer score="20">answer 2</answer>
<answer score="30">answer 3</answer>
<answer score="40">answer 4</answer>
</question>
<question text="question2">
<answer score="1">answer 5</answer>
<answer score="2">answer 6</answer>
<answer score="3">answer 6</answer>
<answer score="4">answer 7</answer>
</question>
</questions>
和PHP代碼如下所示:
<?php
$questions = simplexml_load_file('xml/workshop_evaluation_questions_1.xml');
$questions = json_encode($questions);
$questions = json_decode($questions, TRUE);
$name = $questions['description'];
//echo $name;
//echo '<br>';
$evaluation_order = $questions['evaluation_order'];
//echo $evaluation_order;
//echo '<br>';
foreach ($questions['question'] as $res) {
$text = $res['@attributes']['text'];
foreach ($res['answer'] as $ans) {
//pr($questions) . ' => ';
echo $ans . ' => ';
echo $ans->score[0]; // how to get score???
echo '<br>';
}
echo '<hr>';
}
?>
你能幫我請如何解決這個問題