Getting xml data

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Getting xml data

Post by LDusan »

Hi, I would like to define a var, and then extract some xml data using that var (with AJAX). Pretty simple, but I cannot find any info on this. Any kind of help, like links or advice is greatly appreciated.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Getting xml data

Post by kaszu »

Are you saying you have a 'var' with url

Code: Select all

var url = "/my_file.xml";
and you want to load that xml file using AJAX and then get some data from it?
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting xml data

Post by LDusan »

Something like that, I figured it out by now. The problem was getting the exact xml tag. Thanks anyway...
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Getting xml data

Post by JAB Creations »

Try...

Code: Select all

document.getElementById('test').getElementsByTagName('div')[0]
If you're importing XML via AJAX you need to use importNode.

The above JavaScript will work in all browsers including IE. :)
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting xml data

Post by LDusan »

Thanks a lot, but neither of these 2 works in IE:

Code: Select all

var xmlResponse = xmlHttp.responseXML;
if (!xmlResponse || !xmlResponse.documentElement)
throw("Invalid XML structure:\n" + xmlHttp.responseText);
xmlDocumentElement = xmlResponse.documentElement;
title=xmlDocumentElement.firstChild.data;
document.getElementById("myDivElement").innerHTML=title;

Code: Select all

var xmlResponse = xmlHttp.responseXML;
if (!xmlResponse || !xmlResponse.documentElement)
throw("Invalid XML structure:\n" + xmlHttp.responseText);
var rootNodeName = xmlResponse.documentElement.nodeName;
if (rootNodeName == "parsererror") throw("Invalid XML structure");
xmlRoot = xmlResponse.documentElement;
titleArray = xmlRoot.getElementsByTagName("title");
var html = "";
for (var i=0; i<titleArray.length; i++)
html += titleArray.item(i).firstChild.data + "<br/>";
myDiv = document.getElementById("myDivElement");
myDiv.innerHTML = html;
It reports an object error even though in Firefox both work?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Getting xml data

Post by JAB Creations »

IE8 developer tools, what error do you get and what line? Comment it out and start fixing it from there and remember to use object detection.
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting xml data

Post by LDusan »

I have very little experience with IE developer tools, I don't have a clue where to look at, only thing I see is CSS and DOM stuff, no error reporting...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Getting xml data

Post by Eran »

The microsoft script editor is probably the best way to debug JS on IE. If you have Office, it is included - http://blogs.msdn.com/ie/archive/2004/10/26/247912.aspx
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Getting xml data

Post by JAB Creations »

If an error occurs you'll at least see a yellow triangle at the bottom left corner of IE8.

To access IE8's developer tools press F12 or you'll find it towards the bottom of the tools file menu.

In the developer tools click on the 'script' tag and then click on the 'Star Debugging' button. Sometimes it'll require you to reload the page which is fine.

If an error occurs (be it onload or long after onload has finished) it will automatically ALT+TAB you to the script debugger, show you the exact line where the error occurred, and give you the error message.

So far I'm really loving the developer tools in IE8. Granted I would never rely only on them...Firefox, Opera, and Safari's error consoles are all very useful (Firefox for stricter errors/warnings, Opera with trace-back, and Safari for mime errors come to mind off-hand).
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting xml data

Post by LDusan »

I click on "start debugging" - nothing happens. Not on html, not on js file. No triangle, when I load the page, an alert message pops up which says: "Can't connect to server: Type Error: Object doesn't support this property or method". In Firefox in works great! I appreciate your help Jab Creations.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Getting xml data

Post by JAB Creations »

No problem and try pytrin's suggestion, he's never led me wrong.

Ok you are likely dealing with a JScript issue...that is Microsoft doesn't support JavaScript...they use JScript...their own proprietary implementation of JavaScript...and they use different objects and methods in many instances. You generally have to do object detection. Here is an example...

Code: Select all

if (window.addEventListener){ //standards compliant alert('JavaScript!');}else if (window.attachEvent){ //IE JScript proprietary implementationalert('JScript!');}
To help you please copy the function containing the line where the error occurred in IE's developer toolbar and then we can help you adapt IE support.
Post Reply