0
我們已經有了這種格式的XML文件:訪問元素含量和用SimpleXML解析命名空間屬性
<Quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>0</id>
<title>Ganz Anderer Titel</title>
<questions>
<Question xsi:type="ChoiceQuestion">
<title>Frage1</title>
<id>0</id>
<rightAnswer>1</rightAnswer>
</Question>
<Question xsi:type="ChoiceQuestion">
<title>Frage2</title>
<id>1</id>
<rightAnswer>2</rightAnswer>
</Question>
<Question xsi:type="ChoiceQuestion">
<title>Frage2</title>
<id>2</id>
<rightAnswer>3</rightAnswer>
</Question>
</questions>
<expireDate>2018-06-06T00:00:00</expireDate>
</Quiz>
現在,我們需要解析這個XML文件,但我們不能夠接入內容或屬性的問題元素。
我們正在使用PHP 7內置SimpleXML中的解析器。
echo json_encode($xml->questions);
顯示此
{"Question":[{"title":"Frage1","id":"0","rightAnswer":"1"},{"title":"Frage2","id":"1","rightAnswer":"2"},{"title":"Frage2","id":"2","rightAnswer":"3"}]}
,但我們不知道如何讓每一個問題的數據分別。
用一個簡單的foreach。 –
嘗試一個簡單的'print_r($ xml-> questions)'應該顯示你的數據結構 – RiggsFolly
謝謝。 print_r幫了很大忙。任何想法如何獲得每個問題的xsi屬性? –