ajax call to jsp that returns xml

ajax call to jsp that returns xml

I'm having a heck of time getting jquery, to parse a returned xml from a jsp page.  Any help being able to parse this data would be great.   Or why I cannot parse the data.    If I convert the jsp to xml by removing "<%@ page contentType="text/xml" %>" and renaming it .xml the script works like a charm.

I'm running this on  Apache Tomcat 7.0.2 if that makes any difference.

Here is the Html file with the ajax request.

  1. <html>
  2. <head>
  3. <script src="jquery.js"></script>
  4. <script type="text/javascript">
  5. $('document').ready(function(){
  6. $.ajax({
  7. url: "xml-java.jsp",
  8. type: "GET",
  9. dataType: "xml",
  10. complete: processXml,
  11. contentType: "text/xml;"
  12. });
  13. });
  14. function processXml(xml)
  15. {
  16. var txt = $(xml.responseXml);
  17. alert(xml.responseText);  //works shows the text of the xml
  18. alert(txt.find("stock").length);  //always returns 0
  19. txt.find("stock").each(function() { 
  20. alert($(this).text);
  21. });
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. </body>
  27. </html>

Here is the jsp file "xml-java.jsp"
  1. <%@ page contentType="text/xml" %>


  2. <?xml version="1.0" encoding="UTF-8"?>


  3. <portfolio>
  4.   <stock>
  5.     <symbol>SUNW</symbol>
  6.     <name>Sun Microsystems</name>
  7.     <price>17.1</price>
  8.   </stock>
  9.   <stock>
  10.     <symbol>AOL</symbol>
  11.     <name>America Online</name>
  12.     <price>51.05</price>
  13.   </stock>
  14.   <stock>
  15.     <symbol>IBM</symbol>
  16.     <name>International Business 
  17.     Machines</name>
  18.     <price>116.10</price>
  19.   </stock>
  20.   <stock>
  21.     <symbol>MOT</symbol>
  22.     <name>MOTOROLA</name>
  23.     <price>15.20</price>
  24.   </stock>
  25. </portfolio>