Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parsing XML With jQuery

Posted on 2011-10-25
2
Medium Priority
?
168 Views
Last Modified: 2012-05-12
Hello Experts -
I have the following block of jQuery in a web form:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: "xml.aspx",
            dataType: "xml",
            success: parseXML
        });
        function parseXML(xml) {
            var strOutput;
            $(xml).find('root3').each(function () {
                strOutput = strOutput + $(this).text() + ' - ';
                alert(strOutput);
            });
        }
    });
</script>

Open in new window

For purposes of this question assume that the target XML is properly formed and contains three elements called <root3>, each containing the text 'fubar'.  I would expect the alert within the parseXML function to fire three times, the first displaying 'fubar -', the second displaying 'fubar - fubar -' and the third displaying 'fubar - fubar - fubar -'.  The first firing instead displays 'undefinedFubar -'.  Can someone please tell me why, and what I have to do to get rid of that initial undefined reference?  Thanks much.

-- Rick
0
Comment
Question by:MinnRick
2 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 37028483
first loop :
from line 11, strOutput is undefined, to prevent replace line 11 by : var strOutput = "";
0
 

Author Closing Comment

by:MinnRick
ID: 37031686
Yep, thanks.  That one was a  *duh*  on my part - the variable IS undefined until you give it a value.  Much appreciated.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question