XML document

having an error in Safari loading xml doc firefox it works

Page 1 of 1

9 Replies - 8720 Views - Last Post: 13 April 2010 - 06:24 AM

#1 josephpisciotta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 09

XML document

Posted 03 April 2009 - 01:31 PM

Hi, I am a webmaster trying to implement an auto-content update system without using PHP, which is challenging for me because I use PHP, JAVA, C(and extentions), HTML usually. My current project requires me to not use PHP so I am using javascript and xml.

I have the code written out but I am running into an error with Safari 4.

If you can Please help me thanks in advance.

Here's my code:
<script type="text/javascript">
var req;

function loadXMLDoc(url) {
	var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}

if (xmlDoc!=null)
{ 
xmlDoc.async=false;
xmlDoc.load("test.xml");

document.write("<table border='1'>");

var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{ 
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
}</script>



Is This A Good Question/Topic? 0
  • +

Replies To: XML document

#2 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: XML document

Posted 07 April 2009 - 10:06 PM

What kind of error are you getting? Probe... alert some variables and see what you get.

I'm pretty sure your problem is right here:
var x=xmlDoc.getElementsByTagName("CD");


because the xmlDocument element is not the same as a regular HTML document. So you probably have to access it by saying:
var x=xmlDoc.document.getElementsByTagName("CD");
or something along those lines. But i'm pretty sure that your error is in this line.
Was This Post Helpful? 0
  • +
  • -

#3 josephpisciotta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 09

Re: XML document

Posted 08 April 2009 - 04:47 AM

TypeError: Result of expression 'xmlDoc.load' [undefined] is not a function.

That is the error im getting with Safari. adding the document didnt o anythin

tag*
Was This Post Helpful? 0
  • +
  • -

#4 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: XML document

Posted 08 April 2009 - 09:09 AM

That's interesting. I did some research on Apple's Web Site, and it seems that Safari can't create a document with document.implementation.createDocument. Weird. They have another way of doing it. I loaded a document with Safari 3.2 with this code:

	xmlDoc = new XMLHttpRequest();
	xmlDoc.open("GET", "www.uri.com", false);
	xmlDoc.send();



and the document can be accessed through xmlDoc.responseXML.

This post has been edited by forest51690: 08 April 2009 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 josephpisciotta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 09

Re: XML document

Posted 08 April 2009 - 01:51 PM

thank you very much. by the way is this cross-browser or no?

you seem very knowledgeable can i give you my new full code and help me make it work across browsers.

here:


function displayFullEvents()
{
var currentDate = new Date();
var cuMo = currentDate.getMonth()+1;
var cuDa = currentDate.getDate();
var cuYe = currentDate.getYear();
var req;
var output = "";
var url = "events.xml";
	var xmlDoc=null;
if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}

else if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else
{
alert('Your browser cannot handle this script');
}

if (xmlDoc!=null)
{ 
xmlDoc.async=false;
xmlDoc.load("events.xml");

var x=xmlDoc.getElementsByTagName("EVENT");
var y=xmlDoc.getElementsByTagName("MONTH");
var r=xmlDoc.getElementsByTagName("year");

output += "<table cellpadding=3 cellspacing=2>";
output += "<tr><td colspan=2><center>| ";
for (f=0;f<y.length;f++){
	var month = y[f].getElementsByTagName("number")[0].childNodes[0].nodeValue;
	if((cuMo <= month)){
	output +="<a href='#";
	output += y[f].getElementsByTagName("monthlink")[0].childNodes[0].nodeValue;
	output += "' class='month'>";
	output += y[f].getElementsByTagName("monthname")[0].childNodes[0].nodeValue;
	output += "</a> | ";
	}
	
}
output += "</center></tr></td>";
for (j=0;j<y.length;j++){
var t=y[j].getElementsByTagName("EVENT");
var month = y[j].getElementsByTagName("number")[0].childNodes[0].nodeValue;
if((cuMo <= month)){
	output += "<tr><td colspan=2><b><u>";
	output +="<a id='";
	output += y[j].getElementsByTagName("monthlink")[0].childNodes[0].nodeValue;
	output += "'></a>";
	output +=  y[j].getElementsByTagName("monthname")[0].childNodes[0].nodeValue;
	output += "</u></b></tr></td>";

	for (i=0;i<t.length;i++)
	{ 
		
		var day = t[i].getElementsByTagName("daynum")[0].childNodes[0].nodeValue;
		if ((cuMo == month) && (cuDa <= day)){
		output += "<tr>";
		output += "<td valign='top'>";
		output += t[i].getElementsByTagName("DATE")[0].childNodes[0].nodeValue;
		output += "</td>";
	
		output += "<td>";
		output += t[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
		output += "</td>";
		output += "</tr><tr><td></td></tr>";
		}
		else if ((cuMo < month)){
		output += "<tr>";
		output += "<td valign='top'>";
		output += t[i].getElementsByTagName("DATE")[0].childNodes[0].nodeValue;
		output += "</td>";
	
		output += "<td>";
		output += t[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
		output += "</td>";
		output += "</tr><tr><td></td></tr>";
		}
	}
	output += "<tr align='center'> <TD align='center' colspan='2'><a href='tester.html' class='month'>TOP</a></TD></TR>";
	
}
}

output += "</table>";


}
test = document.getElementById('div1');
test.innerHTML=output;
}


Was This Post Helpful? 0
  • +
  • -

#9 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: XML document

Posted 08 April 2009 - 08:06 PM

No problem. I've used AJAX a couple times now and I think it's a good idea to just create a function that you can reuse. I've created this one, and it's compatible with Safari:

function AJAX_load( /* string */ path)
/*	
	Preconditions: path is valid URI;
	Postconditions: returns a loaded XMLDocument
	Purpose: loads an external XML document and returns it when it loads
		alerts error message if any errors occur
	Adapted from: [url="http://www.w3schools.com/Dom/dom_parser.asp"]http://www.w3schools.com/Dom/dom_parser.asp[/url]
*/	
{
	try
	{ // MSIE
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
	{
		try
		{ // Firefox, Opera, etc.
			var xmlDoc=document.implementation.createDocument("","",null);
		}
		catch(e)
		{
			alert(e.message)
		}
	}
	try
	{
		xmlDoc.async = false; // halt code execution until xmlDoc is loaded
		xmlDoc.load( path );	// load file
	}
	catch(e)
	{ // unable to load
		try
		{	// for Safari
			xmlDoc = new XMLHttpRequest();
			xmlDoc.open("GET", path, false);
			xmlDoc.send();
			xmlDoc=xmlDoc.responseXML;
			
		}
		catch(e)
		{
			alert(e.message)
			return; // return with nothing
		}
	}
	return xmlDoc;
}


What you have to do is make different cases for each browser to define the variable xmlDoc. I actually didn't know about the Safari issue until your post, so thanks for that. With Safari, the doc = document.implementation.createDocument() method is supported, but then you can't do doc.load("file.xml") so you have to make an exception. So Safari goes into the Firefox / Opera section, but then fails where it tries xmlDoc.load(), so then xmlDoc is redefined into an XMLHttpRequest() object and used that way. Also then, you have to use getElementsByTagName() by typing xmlDoc.responseXML.getElementsByTagName(), so I just put this line: xmlDoc = xmlDoc.responseXML

Good luck with your coding. I haven't tested the function thouroughly, so it may have some bugs.

This post has been edited by forest51690: 08 April 2009 - 08:08 PM

Was This Post Helpful? 1

#10 josephpisciotta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 09

Re: XML document

Posted 08 April 2009 - 08:51 PM

Thank you very very much one more question how do you implement that into my code will i still need the if xmlDoc false statement
Was This Post Helpful? 0
  • +
  • -

#11 josephpisciotta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 09

Re: XML document

Posted 09 April 2009 - 08:07 AM

Never mind!!!!

THANK YOU SO MUCH!!!!!!!!!!!!!!!!!!!

If you need any help with any other language i can most likely help im just not pro at javascript.

Thank you again. I will post you the website when I have it online.
Was This Post Helpful? 0
  • +
  • -

#12 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: XML document

Posted 09 April 2009 - 08:25 PM

hey no problem. Good luck with your coding. Javascript is definitely a tough beast to wrangle. But when you're a Javascript beastmaster... it feels good. :)
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Srinivasan S.R.*


Reputation:

Re: XML document

Posted 13 April 2010 - 06:24 AM

Thank you so much for that snippet of code. It has helped me solve out an issue for displaying dynamic content. :-)
I have been stuck with it for the past 3 days.

Thank you very much.
Was This Post Helpful? 0

Page 1 of 1