File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Ajax: responseXML, getElementsByTagName

Parul Aggarwal
Greenhorn

Joined: Mar 27, 2008
Posts: 9
Hi
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.

servlet
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 {
empArray=objAjaxDB.getEmpDetails(strEmpChar);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sbResp.append("<message>");
for(int i=0;i<empArray.size();i++){

sbResp.append("<item><name>"+empArray.get(i)+"</name>");
i=i+1;
sbResp.append("<value>"+empArray.get(i)+"</value></item>");

}
sbResp.append("</message>");

}
resp.setContentType("text/xml");
resp.setHeader("Cache-Control", "no-cache");
System.out.println("String buffer======>"+sbResp.toString());
resp.getWriter().write(sbResp.toString());
}

From Jsp

function getEmpAutoData(emptxt){
var divObj=document.getElementById("newDiv");
var empChar=emptxt.value;
alert("empchar==>"+empChar);
//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 ?
}
}else{
//This is for progress image...indicator.gif is image for loading image ..which will rotate continously
divObj.style.visibility="visible";

}
};
req.open("GET", url, true);
req.send(null);

}//getEmpAutoData

function parseMessageAuto(resXML){

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

divObj.innerHTML="";
//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
if(responseNodes.length>0){
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
}//if(responseNodes.length>0)
}


Please guide. Thank you.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Hey,

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.

http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html

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

Eric
Parul Aggarwal
Greenhorn

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.
Thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Eric
Parul Aggarwal
Greenhorn

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
Parul Aggarwal
Greenhorn

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
Parul
Yasir Bajwa
Greenhorn

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
Greenhorn

Joined: Apr 09, 2008
Posts: 1
Hi,

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...

regards,
Raj.S
Parul Aggarwal
Greenhorn

Joined: Mar 27, 2008
Posts: 9
Hello

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

if(responseNodes.length>0)

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: http://aspose.com/file-tools
 
subject: Ajax: responseXML, getElementsByTagName