-1

我有一个关于将 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]

我的问题是如何做到这一点?

4

2 回答 2

0

谢谢你的答案。我已经这样做了:

var vragenArray = []        

$(this).find("q").each(function() {
                vragenArray.push($(this).text())

    })
于 2012-12-17T11:07:27.620 回答
0

我猜一个数组将是开始的地方?

function vraag() {
    var string = "questions.xml";
    $.get(string, {}, function(xml) {
        $('question', xml).each(function() {
            var question = $(this).find("text").text(),
                id = $(this).attr('id'),
                subject = $(this).find("subject").text(),
                answertype = $(this).find("answertype").text(),
                answers = [];

            $(this).children("answers").each(function(i, ele) {
                answers[i] = $(ele).text();
            });
        });
    });
}​
于 2012-12-13T14:22:40.690 回答