/    Sign up×
Community /Pin to ProfileBookmark

XML Parsing Problem in Firefox.. Pls help!

Guys..
I am stuck with this & it is really confusing as I have tried all possible ways..

I have attached 3 files here – readXML.html, readXML.js and readXML.xml.

I am trying to do something as simple as this.. In my readXML.xml, I have a xml structure that has site name & site value. On my HTML, I allow the user to enter a site name & if that site name matches with the site name in XML (refer JavaScript), the matching site value is used to open a new window. For instance, the user enters CNN and “cnn.com” opens in a new window.

This works in IE but not in Firefox.

Also note I have Firebug installed for my Firefox & when I browse the xmlDom element in the DOM tab, it contains the site name & site values.. But, then why the JavaScript fails to get all the values from XML? Pls try this and can you help me find where the problem is..

Thanks.


*********************************************

readXML.xml


*********************************************

<?xml version=”1.0″ ?>
<sites>
<site>
<name>Hotmail</name>
<value>http://hotmail.com/</value>
</site>
<site>
<name>Yahoo</name>
<value>http://yahoo.com/</value>
</site>
<site>
<name>Gmail</name>
<value>http://gmail.com/</value>
</site>
<site>
<name>CNN</name>
<value>http://cnn.com/</value>
</site>
</sites>


*********************************************


*********************************************

readXML.js


*********************************************

var xmlDoc;
var sites = new Array();

importXML(“readXML.xml”);

// Import the XML
function importXML(file)
{
if (document.implementation && document.implementation.createDocument) // Firefox Check
{
xmlDoc = document.implementation.createDocument(“”, “”, null);
xmlDoc.load(“readXML.xml”);
//xmlDoc.onload = loadXML(xmlDoc);
loadXML(xmlDoc);
}
else if (window.ActiveXObject) // Internet Explorer Check
{
xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async = false;
xmlDoc.load(“readXML.xml”);
//loadXML();
loadXML(xmlDoc);
}
else
{
alert (“Your browser cannot handle the script”);
return false;
}
}

/*
*
Parse through the XML and load the contents in the global array for later use.
*/
function loadXML(xmlDoc)
{
if (xmlDoc != null )
{
var allSitesArray = xmlDoc.getElementsByTagName(“sites”);

if (allSitesArray == null || allSitesArray[0] == null)
{
return false;
}

var sitesArray = allSitesArray[0].getElementsByTagName(“site”);

for (var i = 0; i < sitesArray.length; i ++)
{
var siteName, siteValue = “”;
var siteArray = new Array();

siteNameArray = sitesArray[i].getElementsByTagName(“name”);
siteValueArray = sitesArray[i].getElementsByTagName(“value”);

siteName = siteNameArray[0].firstChild.nodeValue;
siteValue = siteValueArray[0].firstChild.nodeValue;

siteArray[0] = siteName;
siteArray[1] = siteValue;

sites.push(siteArray);
}
}

}

// Open another window with the matching site address, if the site name entered by the user matches the information that is stored.
function showSiteValue()
{
var siteNameInput = document.getElementById(“siteName”).value;
var matchFoundFlag = 0;
for (var i = 0; i < sites.length; i ++)
{
var siteArray = sites[i];
siteName = siteArray[0];
siteValue = siteArray[1];

if (siteName.toLowerCase() == siteNameInput.toLowerCase())
{
matchFoundFlag = 1;
window.open(siteValue);
return false;
}
}

if (matchFoundFlag == 0)
{
alert (“no matches found”);
setFocus();
return false;
}

}

/*
*
Submit the form, if the “Enter” key is pressed.
*/
function formSubmitOnEnterKeyPress(e)
{
// This variable holds the literal character code.
var characterCode;

// If this is a “Netscape” browser, perform the following.
if(e && e.which)
{
characterCode = e.which;
}
// If this is an “Internet Explorer” browser, perform the following.
else
{
characterCode = e.keyCode; //character code is contained in IE’s keyCode property
}

// If the character code is “13”, it means the “Enter” key is hit. Then, return true.
// Else, return false.
if (characterCode == 13)
{
showSiteValue();
return false;
}

}

// Focus on the search text
function setFocus()
{
document.getElementById(“siteName”).focus();
}


*********************************************


*********************************************

readXML.html


*********************************************

<html>
<head>
<title>Search Sites</title>
<script type=”text/javascript” src=”readXML.js”></script>
</head>

<body onload=”setFocus();”>
<form onkeypress=”formSubmitOnEnterKeyPress(event);” method=”post”>
<table>
<tr>
<td>Site : </td>
<td><input type=”text” name=”siteName” id=”siteName” maxlength=”10″ size=”10″ /></td>
</tr>
<tr>
<td colspan=”2″>&nbsp;</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<input type=”submit” name=”submit” value=”Go” onclick=”showSiteValue();” />
</td>
</tr>
</table>
</form>
</body>

</html>
***********************************************?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @sujibharath 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.20,
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,
)...