PDA

View Full Version : issues loading XML in Firefox



russjman
08-14-2005, 07:07 AM
Ive been banging my head on my keyboard for a few hours now with this issue. This about is basic as it can get. Im trying to load XML data and post it to div's with ID's, but when i run it in Firefox the java console says:

"xmlDoc.getElementsByTagName("title").item(0).text has no properties"

It works fine in IE.


html page

<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" >

var article = "news1.xml";
var xmlDoc;
function loadArticle()
{

//for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async="false";
xmlDoc.load(article);
displayXML();
}
//for FF
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("","",null);

xmlDoc.load(article);
displayXML();
}

else if(xmlDoc)
alert('Sorry, this browser is not XML-compliant and cannot render the XML data.');

}

function displayXML(){

document.getElementById("title").innerHTML = xmlDoc.getElementsByTagName("title").item(0).text;
subtitle.innerText= title.innerText + xmlDoc.getElementsByTagName("subtitle").item(0).text;
author.innerText=xmlDoc.getElementsByTagName("author").item(0).text;
main.innerText=xmlDoc.getElementsByTagName("main_text").item(0).text ;
pict_path.innerText=xmlDoc.getElementsByTagName("pict_path").item(0).text;

}
</script>

</head>
<body onload=loadArticle()>



<div id = "title"></div>
<div id = "subtitle"></div>
<div id = "main"></div>
<div id = "author"></div>

</body>
</html>



news1.xml

<?xml version="1.0"?>

<!DOCTYPE article [

<!ELEMENT article (section, pict_path, title, subtitle, author, main_text)>
<!ELEMENT section (#PCDATA)>
<!ELEMENT pict_path (#PCDATA)>
<!ELEMENT author (#PCDATA)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT subtitle (#PCDATA)>

<!ELEMENT main_text (#PCDATA)>

]>




<article>
<section>news</section>
<pict_path>images/current/news1.gif</pict_path>
<title>Chocolate Chip Bars</title>
<subtitle>Chocolate Chip Bars sub</subtitle>
<author>Carol Schmidt</author>
<main_text>
Preheat oven to 350 degrees. Melt butter;
combine with brown sugar and vanilla in large mixing bowl.

Set aside to cool. Combine flour, baking powder, and salt; set aside.

Add eggs to cooled sugar mixture; beat well. Stir in reserved dry
ingredients, nuts, and chips.

Spread in greased 13-by-9-inch pan. Bake for 25 to 30 minutes until golden
brown; cool. Cut into squares.

</main_text>
</article>
:confused: :mad: :(

russjman
08-17-2005, 08:57 PM
Have i truly stumped the Guru's of Dynamic Drive?!?!?

Lucky_Koen
08-25-2005, 09:24 AM
I have had the same problem and just found out how to get it working. Apparently the function that processes the XML was called before the xml document was completely loaded. The following code should work for you, it checks if the document is loaded and will not calls displayXML() before that.


if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("","",null);
var isLoaded = xmlDoc.load(article);
if (isLoaded == true) { displayXML(); }
}

You will need to make a similar check for IE :


if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async="false";
while (xmlDoc.readyState != 4) {} // readyState will be set to 4 when the document is loaded
xmlDoc.load(article);
displayXML();
}