Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading xml in Javascript fails

 
karthick ryan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The following piece of script does not work in jsp. Please let me know what could be the reason.

var xmlObj;
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async = false;
xmlDoc.load("web.xml");//This xml file is present in the folder where the jsp file is present
xmlObj = xmlDoc.documentElement;
alert('obj '+xmlObj);// I get a null here

Thanks!!
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would never load a XML filethat way. I would use the XMLHttpRequest Object.

See if this helps you debug: http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html

Eric
 
karthick ryan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your forum,
.."I have talked about this in the past: http://radio.javaranch.com/pascarello/2005/10/26/113034827699 6.html, but I thought I would put some more information in how to debug this error. .."


The above link does not work..

Can you please get me the sample code for the same.
Please !!!
[ August 02, 2007: Message edited by: karthick ryan ]
 
karthick ryan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your suggestion, I used this :

var xmlhttp
loadXMLDoc('web.xml')
function loadXMLDoc(url)
{
alert(url);
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
else
{
alert("Your browser does not support XMLHTTP.")
}
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
alert("XML data OK")
}
else
{
alert(xmlhttp.status);
alert("Problem retrieving XML data:" + xmlhttp.statusText)
}
}
}

I get xmlhttp.status=404. Where is it searching for the XML file? I still have the problem.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your code, the web.xml file must be present in the root folder of your web application and not in any sub-directoty thereof. You mentioned the web.xml file was located in the same folder as the JSP file, right?
 
karthick ryan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The web.xml file is present inside the same folder as that of the JSP. Now is there anyway I can find the root directory path via javascript?
 
karthick ryan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pheeeeew...
Its all done in terms of fetching the XML.
Cooooool !!!
I just put the web.xml file in the previous directory and used a "..\web.xml" to load the XML.. It works!!!

Next job is to read the contents of the XML.

Thank you to all javaranch.com people for helping me solve this one.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic