File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Ajax: responseXML, getElementsByTagName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax: responseXML, getElementsByTagName" Watch "Ajax: responseXML, getElementsByTagName" New topic

Ajax: responseXML, getElementsByTagName

Parul Aggarwal

Joined: Mar 27, 2008
Posts: 9
I have just started with ajax. I am trying to build an application for autocompletion.
We enter a lettr and names starting with that letter should be displayed in div tag.

I am getting correct response but am not able to parse it to show it in the div tag.
I think I am not using getElementsByTagName correctly.

Here is the code from servlet and jsp.

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String strHidden=req.getParameter("hidden");
System.out.println("Entring into Traditional Ajax Conmtroller==================");
AjaxDB objAjaxDB=new AjaxDB();
StringBuffer sbResp=new StringBuffer();
if(strHidden!=null && strHidden.equalsIgnoreCase("empauto")){
String strEmpChar=req.getParameter("emp");

ArrayList empArray=new ArrayList();
try {
} catch (SQLException e) {
// TODO Auto-generated catch block
for(int i=0;i<empArray.size();i++){



resp.setHeader("Cache-Control", "no-cache");
System.out.println("String buffer======>"+sbResp.toString());

From Jsp

function getEmpAutoData(emptxt){
var divObj=document.getElementById("newDiv");
var empChar=emptxt.value;
//var params=encodeURI("hidden=empauto&emp="+empChar);
var url="TraditionalAjaxController?hidden=empauto&emp="+empChar;
var req=initRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {

//if response comes correctly..(if no exception in middle of processing)
parseMessageAuto(req.responseXML);// Is this correct ?
//This is for progress image...indicator.gif is image for loading image ..which will rotate continously"visible";

};"GET", url, true);


function parseMessageAuto(resXML){

var divObj=document.getElementById("newDiv");

//var root=resXML.documentElement;

var responseNodes=resXML.getElementsByTagName("message")[0];
alert("response node===>"+responseNodes);
// here it fails the length is 0 here I cannot proceed further
var responseNode=responseNodes[0];
alert("responseNode" + responseNode);
var itemNodes=responseNode.getElementsByTagName("item");
for(var i=0;i<itemNodes[i].length;i++){
var nameNodes=itemNodes[i].getElementsByTagName("name"); //getting the elements with tagname as <name> from xml response
var valueNodes=itemNodes[i].getElementsByTagName("value"); //getting the elements with tagname as <value> from xml response
if(nameNodes.length>0 && valueNodes.length>0){
var name=nameNodes.childNodes[0].nodeValue;//represents ID of employee
var value1=valueNodes.childNodes[0].nodeValue;//represents Name of employee

}//for close

Please guide. Thank you.
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385

One thing that would help out a lot if to us code tags around your code. It preserves the format and makes is a lot easier to read it.

When looking at your code, I think I know what the issue is.

Step 4 might point it out for you. I think you are missing one important line on top of your XML document.

Parul Aggarwal

Joined: Mar 27, 2008
Posts: 9
Thanks Eric, I'll take care.

Could you please explain me Point 4 with some example, a psuedo code.

I cant identify which important line I m missing from my generated xml document and how to open it directly into the browser.

Please be patient with me.
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
If you are using a post to open it up, you probably need to just hard code some values on the server. You than type the direct addy to the file that the Ajax call calls.

I do not see the <xml..... line

Parul Aggarwal

Joined: Mar 27, 2008
Posts: 9
Hi Eric

This is what I get as response. There is no syntax error whether I write
<?xml version="1.0" encoding="UTF-8" ?> or not.

However when I start my Tomcat Ver 5.5.26, then on my tomcat console during initialization it shows "org.apache.catalina.core.StandardHost start
INFO: XML validation disabled. Can this be the reason that I am not able to parse the result.

Parul Aggarwal

Joined: Mar 27, 2008
Posts: 9
continued ...

If XML validation disabled is the problem then pls tell me the solution

Also I am giving the code in proper format. please see..

Thank you
Yasir Bajwa

Joined: May 02, 2007
Posts: 23
Hard to say what's going on there but instead of

alert(resXML);// This returns OBJECT

Try to also look at the responseText property (req.responseText)to see the text that is coming back.

You might also want to try firefox and download the firebug plugin. You can then set break points and step through the code to inspect values.
sheela raj

Joined: Apr 09, 2008
Posts: 1

Once you got the responseXML object, then get the root element from that as shown in below..
var root=resXML.documentElement;
then using the "root" element you can get root/other elements by their tag name
as follows.....
var responseNodes=root.getElementsByTagName("message")[0];

I think this may help you...

Parul Aggarwal

Joined: Mar 27, 2008
Posts: 9

Raj. S - > I have tried this also
var responseNodes=root.getElementsByTagName("message")[0];
however at


statement it gives scripting error ---> responseNodes is null

If I remove [0] from it then also it doesn't pass the if test.
i have placed alert also

I have tried my xml on both IE and Mozilla...
I m badly struck..

Someone please help me ..
I agree. Here's the link:
subject: Ajax: responseXML, getElementsByTagName
jQuery in Action, 3rd edition