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.
- <html>
- <head>
- <script src="jquery.js"></script>
- <script type="text/javascript">
- $('document').ready(function(){
- $.ajax({
- url: "xml-java.jsp",
- type: "GET",
- dataType: "xml",
- complete: processXml,
- contentType: "text/xml;"
- });
- });
-
- function processXml(xml)
- {
- var txt = $(xml.responseXml);
- alert(xml.responseText); //works shows the text of the xml
- alert(txt.find("stock").length); //always returns 0
- txt.find("stock").each(function() {
- alert($(this).text);
- });
- }
- </script>
- </head>
- <body>
- </body>
- </html>
Here is the jsp file "xml-java.jsp"
- <%@ page contentType="text/xml" %>
- <?xml version="1.0" encoding="UTF-8"?>
- <portfolio>
- <stock>
- <symbol>SUNW</symbol>
- <name>Sun Microsystems</name>
- <price>17.1</price>
- </stock>
- <stock>
- <symbol>AOL</symbol>
- <name>America Online</name>
- <price>51.05</price>
- </stock>
- <stock>
- <symbol>IBM</symbol>
- <name>International Business
- Machines</name>
- <price>116.10</price>
- </stock>
- <stock>
- <symbol>MOT</symbol>
- <name>MOTOROLA</name>
- <price>15.20</price>
- </stock>
- </portfolio>