[jQuery] Translate a website using XML file : how to parse xml with Jquery ?
Hello,
I would like to use Jquery to parse an xml file. For the moment I use
the following javascript code which works with IE, FF and Opera but
NOT with Safari (Mac).
Jquery is compatible with Safari, so I would like to do the same thing
with this librairy but I don't know how to get the "tagname" in a xml
file.
With the following jquery code, I can get the contains of a tag, but
it's not the tagname :
var IpAddress = $("IpAddress", xml).text();
$("div#IpAddress").html(IpAddress);
Anyone can help me ?
Thanks in advance.
Benoit
----------------------------
My code :
My XML file (english.xml) :
-------------------------------------
<prompt>
<IpAddress>Ip Address :</IpAddress>
<SubNetMask>SubNet Mask :</SubNetMask>
<English>English :</English>
<French>French :</French>
</prompt>
My HTML file :
--------------------
<html>
<head><script language="javascript" src="translation.js"></script></
head>
<body onload="loadXML()">
<span id="IpAddress"></span>195.168.10.20
<span id="SubNetMask"></span> 255.255.255.0
</body>
</html>
My javascript code (translation.js) :
--------------------------------------------------
var xmlDoc;
function loadXML()
{
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("english.xml");
translateall();
}
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.load("english.xml");
xmlDoc.onload = translateall;
}
else
{
alert('Your browser cannot handle this script');
}
}
function translateall()
{
var elem;
var Prompts = new Array;
var x = xmlDoc.documentElement;
for (i=0 ; i<x.childNodes.length ; i++)
{
Prompts[i] = x.childNodes[i].nodeName;
}
for (i=0; i<Prompts.length; i++)
{
elem = document.getElementById(Prompts[i]);
if (elem)
document.getElementById(Prompts[i]).innerHTML =
xmlDoc.getElementsByTagName(Prompts[i])[0].childNodes[0].nodeValue;
}
}