/    Sign up×
Community /Pin to ProfileBookmark

Loading a xml file

Hi everyone. I’m using this code, wich I got from the web (I can’t find the source yet, but I’m still looking for it to post it):

[CODE]
<script type=”text/javascript”>
var xmlDoc;
function parseXML()
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument(“”,””,null);
}
catch(e)
{
alert(e.message);
return;
}
}

xmlDoc.onload = init;
xmlDoc.async = false;
xmlDoc.load(“code.xml”);

}

function init()
{
alert(xmlDoc.getElementsByTagName(“keywordlist”).length);
}
[/CODE]

My xml file is quite simple:

<?xml version=”1.0″ encoding=”utf-8″?>
<keywordlist>
<keyword>dim</keyword>
<keyword>false</keyword>
<keyword>true</keyword>
</keywordlist>

Alert window shows me 0 as length of elements with tagname keywordlist. If I try to access any other properties of methods of XML DOM object, it sometimes return me null things and other times just doesn’t show logical results i.e. if I call xmlDoc.documentElement.childNodes[0].nodeValue, it returns me null. What’s wrong with this code?. Thanks for advance. Regards.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ScriptageMay 23.2008 — Microsoft XMLDOM doesn't support .onload; instead use onreadystatechange:

[CODE]<script type="text/javascript">
var xmlDoc;
function parseXML()
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function(){

if(xmlDoc.readyState == 4){

init();

}

}
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.onload = init;
}
catch(e)
{
alert(e.message);
return;
}
}

xmlDoc.async = false;
xmlDoc.load("code.xml");


}

function init()
{
alert(xmlDoc.getElementsByTagName("keyword").length);
}

parseXML();

</script>[/CODE]


Regards

Carl
Copy linkTweet thisAlerts:
@mariano_donatiauthorMay 23.2008 — Is good to know that. I've changed my code. But I'm testing it with Firefox, so that block of code isn't executed at all. It seems really really weird to me. I don't have a clue about how to solve it.
Copy linkTweet thisAlerts:
@mariano_donatiauthorMay 23.2008 — I think may be I'm using xml dom object in a wrong way. How do we get the text between a xml element? i.e. I want to get 'true' (wich is the text of the keywordlist's third element). I'm doing it now with this:

xmlDoc.documentElement.childNodes[2].nodeValue;

is it right?
Copy linkTweet thisAlerts:
@mariano_donatiauthorMay 23.2008 — If I use node.text property I get the right value, but it only works with IE, if I try to do the same testing it with firefox I received 'undefined'. If I try to get the value like node.nodeValue, I get null in both Firefox and IE.
Copy linkTweet thisAlerts:
@mariano_donatiauthorMay 23.2008 — Well I found on the web that text is not supported by firefox, I need to use textContent instead. But, here is the quid: how do I suppose to write a code that works in all browsers? Do I have to write a conditional statement every time I need to get a tag value? Is there a method wich works in both Firefox and IE?. So much questions by now...
×

Success!

Help @mariano_donati spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...