2

I'm trying to get a part of an xml file into a javascript array. I've tried verry much tutorials, but I just can't get it working. Here's the part of the xml file i want to use:

    <answers>
      <answer id="0">hello</answer>
      <answer id="1">goodbye</answer>
    </answers>

I want to use the answers in a javascript array like:

var FAnswers = new Array("hello","goodbye");

Is there anybody, who knows a simple little javascript that does the job?

2
  • you just created an array FAnswers with 2 elements. It seems like you've answered your own question. Commented Jan 22, 2012 at 13:54
  • 1
    @Jason I believe the question is about how to parse the XML into the array that. Commented Jan 22, 2012 at 13:59

4 Answers 4

4

The following function should do the trick:

function get_answers_from_xml_string(xml_string) {
    // Parse the XML string into a XMLDocument
    var doc = window.DOMParser
                ? new DOMParser().parseFromString(xml_string, 'text/xml')    // Standard
                : new ActiveXObject('Microsoft.XMLDOM').loadXML(xml_string); // IE

    // Find the answer nodes
    var answers_nodes = doc.getElementsByTagName('answer');
    var answers = [];

    // Loop through them and save their text content into an array
    for (var i = 0; i < answers_nodes.length; i++) {
        answers.push(answers_nodes[i].firstChild.data)
    }

    return answers;
}

Just pass it a string containing the XML with answers and it will return an array:

var xml_string = '<answers><answer id="0">hello</answer><answer id="1">goodbye</answer></answers>';
var answers = get_answers_from_xml_string(xml_string);
// answers == ["hello", "goodbye"]
Sign up to request clarification or add additional context in comments.

3 Comments

I might be wrong, but in the second part, do i have to know what the answers are, in advance? I need to know, because my xml file will change by user upload.
I am not sure if I understand your question correctly, but you don't need to know the actual answers in advance, that's what you have the function for. It will extract answers from a XML string for you.
I thought i would have to know them in advance, because this rule of code: 'var xml_string = '<answers><answer id="0">hello</answer><answer id="1">goodbye</answer></answers>';', doesn't it care what's in that rule? Another question, where in the code does the link to the xml file has to be?
2

So you need to parse XML in javascript. Take a look at this example I've just made:

// Some XML, say loaded with AJAX, etc.
var str = '<response><answers><answer id="0">hello</answer><answer id="1">goodbye</answer></answers><data id="2341-63">Test data</data></response>';

var parser, xml;
if (window.DOMParser) {
    parser = new DOMParser();
    xml = parser.parseFromString(str, 'text/xml');
}
else { // IE
    xml = new ActiveXObject('Microsoft.XMLDOM');
    xml.async = false;
    xml.loadXML(str);
}

var nodes = xml.getElementsByTagName('answer');

var i, l = nodes.length, answers = [];
for (i = 0; i < l; i++) {
    answers.push(nodes[i].childNodes[0].nodeValue);
}

console.log(answers) // ["hello", "goodbye"]

Comments

0

According to the answer to this question you need to do something like that (assuming you load your XML via XHR):

// get XML 
var xml = xhr.responseXML;
var target = new Array();

// get answers
var answers= xml.getElementsByTagName("answer");
for (var i = 0; i < answers.length; i++) {   
    target.push(answers[i].nodeValue);
}  

Comments

0
How about this way: The simplest - 

 var xml = "<answers><answer id='0'>hello</answer><answer id='1'>goodbye</answer></answers>";
    document.write (xml);
    var doc = document.getElementsByTagName('answer');
    var arrayAnswers = [];
    for(var i=0, len = doc.length; i < len; i++)
    {
       arrayAnswers.push(doc[i]);
    }
    alert(arrayAnswers[0].innerHTML);
    alert(arrayAnswers[1].innerHTML);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.