Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:36:45+00:00 2026-05-18T20:36:45+00:00

I have the following XML file: <?xml version=’1.0′ encoding=’utf-8′?> <Answers> <Question1 q=What is your

  • 0

I have the following XML file:

<?xml version='1.0' encoding='utf-8'?>
<Answers>
    <Question1  q="What is your favourite colour?&lt;br /&gt;">
        <a1>Blue</a1>
        <a2>purple</a2>
        <a3>Green</a3>
        <a4>Red</a4>
        <a value="0">0</a>
        <b value="0">0</b>
        <c value="1">0</c>
        <d value="0">0</d>
    </Question1>
    <Question2  q="What is the colour of the grass?&lt;br /&gt;">
        <a1>Blue</a1>
        <a2>Green</a2>
        <a3>Purple</a3>
        <a4>Red</a4>
        <a value="0">0</a>
        <b value="1">0</b>
        <c value="0">0</c>
        <d value="0">0</d>
    </Question2>
    <Question3  q="What is the average air speed of an unladen swallow?&lt;br /&gt;">
        <a1>10mph</a1>
        <a2>15mph</a2>
        <a3>30mph</a3>
        <a4>European or African swallow?</a4>
        <a value="0">0</a>
        <b value="0">0</b>
        <c value="0">0</c>
        <d value="1">0</d>
    </Question3>
</Answers>

And I am parsing it with the following code:

function runQuiz(whichQ) {
            if (window.XMLHttpRequest) { 
                xmlhttp = new XMLHttpRequest(); 
            } else { 
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //If browser == IE, get ActiveX object
            } 
            xmlhttp.open("GET", whichQ, false);  //Open the file using the GET routine
            xmlhttp.send();  //Send request
            xmlDoc = xmlhttp.responseXML;  //xmlDoc holds the document information now

            var atext,btext,ctext,dtext = 0;

            $(xmlDoc).find("Question1").each(function() {           
                var a = xmlDoc.childNodes[0].children[0];

                $('#form').prepend( $(xmlDoc).find("Question1").attr('q') ); //Append Question

                $(a).find("a1").each(function() {
                    $('#form').append('<input type="radio" name="answer1" class="a1" value="15mph"> 15mph<br />');
                    atext = $(this).parent().children('a').text();
                });
                var a = xmlDoc.childNodes[0].children[0];
                $(a).find("a2").each(function() {
                    $('#form').append('<input type="radio" name="answer2" class="a2" value="20mph"> 20mph<br />');
                    btext = $(this).parent().children('b').text();
                });
                var a = xmlDoc.childNodes[0].children[0];
                $(a).find("a3").each(function() {
                    $('#form').append('<input type="radio" name="answer3" class="a3" value="15mph"> 30mph<br />');
                    ctext = $(this).parent().children('c').text();
                });
                var a = xmlDoc.childNodes[0].children[0];
                $(a).find("a4").each(function() {
                    $('#form').append('<input type="radio" name="answer4" class="a4" value="eurofrican"> European or African Swallow?<br />');
                    dtext = $(this).parent().children('d').text();
                });

                $('.label, .cu-mid p').fadeOut(150); //Fade out the letters on the bars and the overhead labels
                $('.cu-mid','.cuboid.'+'green1').animate({ height: atext*2 }, 550,'easeOutBounce'); //animate 1
                $('.cu-mid','.cuboid.'+'green2').animate({ height: btext*2 }, 550,'easeOutBounce'); //animate 2
                $('.cu-mid','.cuboid.'+'green3').animate({ height: ctext*2 }, 550,'easeOutBounce'); //animate 3
                $('.cu-mid','.cuboid.'+'green4').animate({ height: dtext*2 }, 550,'easeOutBounce', function() { //animate 4 then...
                    $('.label1').html(atext+'%'); //Set all four labels
                    $('.label2').html(btext+'%');
                    $('.label3').html(ctext+'%');
                    $('.label4').html(dtext+'%');
                    $('.label, .cu-mid p').fadeIn(150); //Fade it all back in
                });
            });
        }

So my question is: Is there a better/more efficient/elegant way of doing this?

I am iterating through the XML document and retrieving the child nodes of other elements.

Any and all ideas welcome! 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:36:45+00:00Added an answer on May 18, 2026 at 8:36 pm
    1. $(a), $('#form') is called multiple times – use variables.
    2. var a = xmlDoc.childNodes[0].children[0] called multiple times – why?
    3. Do you need these animations?!
    4. Generally you append some info as input tags with hardcoded text. Why you don’t use text from XML?
    5. This code is binded to data too much. What if there are 5 questions? If you have control over this XML I would suggest redesign.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following xml file as input .... <?xml version=1.0 encoding=ISO-8859-1?> <T0020 xsi:schemaLocation=http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd
Suppose I have the following XML file, and I want to use PowerShell (version
sI have an XML file that looks like the following... <a> <b> <version>1.0</version> <c>
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following xml I'd like to deserialize into a class <?xml version=1.0
I have made following changes to the POM.xml file for adding a manifest file
I have the following code snippet in my feature.xml <Feature Id=0c272f81-49c0-440a-a544-677d51a8ffe4 ReceiverAssembly=FeaturedMasterPage, Version 1.0.0.0,
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.