Welcome to WebmasterWorld Guest from 34.203.245.76

Forum Moderators: open

Message Too Old, No Replies

Using JavaScript to retrive XML in IE and FireFox

Using JavaScript to retrive XML in IE and FireFox

     
11:57 am on Feb 14, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:June 2, 2003
posts:59
votes: 0


Hi all..

I have been struggling with this for a while now.
What I am trying to do is get info from an XML page using JavaScript.
I have found loads of code, and have tried almost everything to get it to work in both IE and FF...
But with no success...

I have code that works in FF but not in IE

My JS Code:
//******************************************************
var xmlDoc;
var txt_head = "";
var txt_content = "";

function loadXML(str_xmlelement, int_functype)
{
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("helpContent.xml");

if (int_functype == "1") {
getXMLhead(str_xmlelement);
}
else if (int_functype == "2") {
getXMLbody(str_xmlelement);
}
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("helpContent.xml");

if (int_functype == "1") {
xmlDoc.onload=getXMLhead(str_xmlelement);
}
else if (int_functype == "2") {
getXMLbody(str_xmlelement);
}

}
else
{
alert('Your browser cannot handle this script');
}
}

function getXMLhead(str_xmlelement) {
alert("no ");
var x=xmlDoc.documentElement.childNodes;

//document.getElementByTagname("person").attributes.getNamedItem("name").value

for (var I=0;i<x.length;i++)
{
if (x[I].tagName == str_xmlelement)
{
//Process only element nodes
txt_head = x[I].childNodes[1].firstChild.nodeValue;
}
}
}

function getXMLbody(str_xmlelement) {
alert("way ");
var x=xmlDoc.documentElement.childNodes;

for (var I=0;i<x.length;i++)
{
if (x[I].tagName == str_xmlelement)
{
txt_content = x[I].childNodes[3].firstChild.nodeValue;
}
}
}
//******************************************************

What I am trying to do:
I have a function(1) that calls a function(2) that get the data from an XML file..
Makes sense?

Help?

9:42 am on Feb 18, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Feb 12, 2006
posts:224
votes: 0


replace

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

with

try
{
xmlDoc=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlDoc=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
return false;
}
}

I'm not sure if it will solve your problem but it may do.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members