• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with AJAX and XML

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am unable to parse XML data sent to an AJAX call from a servlet.
The data passes into the AJAX call but I get NULL when i try to
parse it .

JSP page
--------

<script type="text/javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function startRequest()
{

createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET","chat", true);
xmlHttp.send(null);
}

function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
alert(xmlHttp.responseText);
var xmlDoc = xmlHttp.responseXML;

var northNode = xmlDoc.getElementsByTagName("message")[0];

var northStates = northNode.getElementsByTagName("type");

var currState=northStates[0];

var temp="np";

temp=currState.childNodes[0].nodeValue;
alert(temp);
}
}
}

function listNorth()
{

var xmlDoc = xmlHttp.responseXML;

var northNode = xmlDoc.getElementsByTagName("message")[0];

var northStates = northNode.getElementsByTagName("type");

var currState=northStates[1];

var temp=currState.childNodes[1].nodeValue;

}

</script>

Servlet
--------
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

System.out.println("Reaching the servlet");

PrintWriter out = response.getWriter();

out.print("<?xml version=1.0 encoding=UTF-8?>");
out.print("<message>");
out.print("<type>House</type>");
out.print("</message>");
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

What is null - xmlDoc? northNode? northStates? currState? temp?

I'm not sure if it matters, but the XML being sent by the servlet doesn't look well-formed - it has no double quotes around the attributes.

Also, you're not setting the transfer encoding of the servlet output. I don't think it matters here, but if the XML claims to be in UTF-8, then you need to make sure that what the servlet sends really is UTF-8.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic