Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load XML (Firefox)

Status
Not open for further replies.

Reinout

IS-IT--Management
Feb 22, 2004
48
BE
Hi, I got a question about loading an XML file in javascript. I'm using it to create a table dynamically.
Everything's working fine using I.E., the structure file of the XML file has been loaded correctly into firefox as well, since I can see the names of the nodes. It's only the data that seems to be missing, since I always get "undefined" when I try to call for a text or a value (which is not the case in I.E.)
I searched for this problem on the internet for a while but can't seem to find something that really helps me.


Code:
function loadXML(file) {
  var moz = (typeof document.implementation != 'undefined') &&
            (typeof document.implementation.createDocument != 'undefined');
  var ie =  (typeof window.ActiveXObject != 'undefined');
  var xmlDoc;

  if (moz)
    {xmlDoc = document.implementation.createDocument("", "", null);
	xmlDoc.async = false;
	}
  else if (ie)
    {xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async = false;
	}
  xmlDoc.load(file);
  return xmlDoc;
  }


function buildTablerows(xmlFile)
{
var xmlDoc = loadXML("Data.xml");
var root = xmlDoc.documentElement;
xmlObj=xmlDoc.documentElement;

var cn=xmlDoc.getElementsByTagName('Beeld');

//document.write(xmlObj.childNodes.length)
document.write(cn.length);
tekst="<TR align='center'>";
alert(xmlDoc.getElementsByTagName('Beeld')[0].getElementsByTagName('Uitleg') + ' ' + root.text);//[0].text);

var j = 0;
//for(i=0; i<xmlObj.childNodes.length; i++)
for(i=0; i<cn.length; i++)
{		
tekst += "<TD width='128' align='center' valign='top' class=title2original > <a href='' ><div align='center'><img src='";
tekst +=  root.getElementsByTagName("Beeld")[i].getElementsByTagName("Item")[0].text; //"bradbrook.jpg";
//xmlObj.childNodes(i).childNodes(0).text;
tekst += "' width=128 height=192 border=0 align='middle'>";
tekst += "</a><br><a href=''>";
tekst += root.getElementsByTagName("Beeld")[i].getElementsByTagName("Uitleg")[0].nodeName; //nodeName; //root.nodeName; 
//xmlObj.childNodes(i).childNodes(1).text;
tekst += "</a><br></TD>";
j += 1;
if(j == 5)
	{
	j = 0;
	tekst += "</TR><TR align='center'>";
	}
}
if(j>0 && j < 5)
{
	for(i=j; i<5; i++)
	{
	tekst += "<TD width='128' align='center' valign='top' class=title2original>&nbsp;<div align='center'></TD>";
	}
}
tekst += "</TR>";
document.write(tekst);
}



The XML file :

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<Produkt>
  <Beeld>
    <Item>bradbrook.jpg</Item>
    <Uitleg>Uitleg 1</Uitleg>
  </Beeld>
  <Beeld>
    <Item>bradbrook2.jpg</Item>
    <Uitleg>Uitleg 2</Uitleg>
  </Beeld>
</Produkt>


HTML :
Code:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript" src="menu/javascript/Laden3.js"></script>
</head>

<body>

<DIV><TABLE width=750><TBODY align='center'><script>bouwNieuws('Data.xml')</script></TBODY></TABLE></DIV>


</body>
</html>
 

Where is the bouwNieuws function?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Never mind... just replace ".text" with ".firstChild.nodeValue". It looks like ".text" is not cross-browser.

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I changed bouwNieuws into buildTablerows to make it easier to understand. I'll try the .firstchild.nodeValue :)
 
Thanks a lot, it's like you say, .text was guilty !
 
Any idea why it still isn't working in Opera?
 
It appears the error is in the xmlDoc.load(file) line?
 
Any idea why it still isn't working in Opera?

Yes - because you're specifically only instantiating the XML DOM if the browser is Mozilla or IE:

Code:
if (moz) {
   ...
} else if (ie) {
   ...
}

You have no branch for Opera.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
That's incorrect Dan.

If I put this :

Code:
function loadXML(file) {
  var moz = (typeof document.implementation != 'undefined') &&
            (typeof document.implementation.createDocument != 'undefined');
  var ie =  (typeof window.ActiveXObject != 'undefined');
  var xmlDoc;

  if (moz)
    {xmlDoc = document.implementation.createDocument("", "", null);
	xmlDoc.async = false;
		//var loaded = xmlDoc.load(file);
		//if (loaded) {			
		//parsexml();
		//}
	alert('Moz');
	}
  else if (ie)
    {xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async = false;
	//xmlDoc.onreadystatechange = function () {
		//if (xmlDoc.readyState == 4) parsexml();
		//}
	//xmlDoc.load(file);
	//alert('Opera');
	}
  else
    {
	alert('Opera');
	}
  xmlDoc.load(file);
  return xmlDoc;
  }

I get a msgbox saying 'Moz' in Firefox AND in Opera. Moz is just the name of the variable, which is both for opera & firefox (and netscape as well I think).
So I try to instantiate the XMLDoc but it fails to load.

What I get in the javascript console in Opera:

Code:
file: ...Test6.htm
Inline script thread
Error:
name: TypeError
message: Statement on line 29: Type mismatch (usually a non-object value used where an object is required)
Backtrace:
  Line 29 of linked script .../menu/javascript/Laden3.js
    xmlDoc.load(file);
  Line 36 of linked script .../menu/javascript/Laden3.js
    var xmlDoc = loadXML("Data.xml");
  Line 1 of inline#1 script in ...Test6.htm
    buildTableRows("Data.xml");
 

Apologies - I should have read your code more thoroughly before replying.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top