我有一个关于将 xml 子文件放入 javascript 数组的问题。我有以下 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<questionaire>
<title>Festival enquete</title>
<author>Lars Groot </author>
<description>Demografische vragen</description>
<question id="0">
<subject>demografie</subject>
<note>Deze vraag is eigenlijk bedoeld om mensen op het verkeerde been te zetten</note>
<text>Wat is uw leeftijd?</text>
<answertype>meerkeuze</answertype>
<answers>
<q>100</q>
<q>200</q>
<q>300</q>
</answers>
</question>
<question id="1">
<text>Waar komt u vandaan</text>
<answertype>openbox</answertype>
</question>
<question id="2">
<text>Wat is uw geslacht</text>
<answertype>meerkeuze</answertype>
<answers>
<q>Vrouwtje<q>
<q>Mannetje<q>
</answers>
</question>
<question id="3">
<text>Waarom stel ik deze vraag</text>
<answertype>meerkeuze</answertype>
</question>
</questionaire>
我想在 javascript 数组中推送由 q 标记的答案。我已经使用下一个代码来获取孩子:
function vraag(){
string = "questions.xml";
$.get(string,{},function(xml){
$('question',xml).each(function(){
question = $(this).find("text").text();
id = $(this).attr('id');
subject = $(this).find("subject").text();
answertype = $(this).find("answertype").text();
answer = $(this).children("answers").text();
现在我想把答案放在一个 javascript 数组中。所以 q 100 是 answer[0],q 200 是 answer[1] 并且 q 300 是 answer[2]
我的问题是如何做到这一点?