Connecting Tech Pros Worldwide Help | Site Map

Parsing XML with namespace in IE. (getElementsByTagNameNS())

  #1  
Old November 2nd, 2008, 08:15 PM
Steve
Guest
 
Posts: n/a
With the help of this newsgroup and Google I have got this code
working fully in Firefox and can alert the XML in IE but because IE
does not impliment the DOM "getElementsByTagNameNS()" function I
cannot read the individual rates from the Cube namespace.

Is there a wrapper or some other relatively simple method of getting
IE to do what in Firefox is straighforward?

Here is the code. Any help gratefully received.

var doc
function load() {
if (document.implementation &&
document.implementation.createDocument){
doc = document.implementation.createDocument("", "", null);
doc.load('CEBrates.xml');
doc.onload = createTable;
}
else if (window.ActiveXObject){
var doc1 = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile){
doc1.async="false";
doc1.onreadystatechange=verify;
doc1.load(xmlFile);
doc=doc1.documentElement;
}
loadXML('CEBrates.xml');
alert(doc.xml)
}
else {
alert('Your browser can\'t handle this script');
return;
}
}
function verify() {
if (doc1.readyState != 4 ){
return false;
}
}
function createTable() {
var cubes = doc.getElementsByTagNameNS('http://www.ecb.int/
vocabulary/2002-08-01/eurofxref','Cube');
var dateRate = cubes[1].getAttribute('time');
var dateRateSplit = dateRate.split('-');
for (var i = 2; i < cubes.length; i++){
var currency = cubes[i].getAttribute('currency');
var rate = cubes[i].getAttribute('rate');
rateObject[currency] = rate;
}
document.getElementById('boldStuff').innerHTML = dateRateSplit[2]
+ "." + dateRateSplit[1] + "." + dateRateSplit[0];
document.getElementById("currency").value = "GBP";
getRates();
};
var rateObject = {};
function getRates() {
var curr= document.getElementById("currency").value;
var currRate = rateObject[curr];
var currStatement= "1 EUR = " + currRate + " " + curr ;
document.getElementById("rate").value=currStatemen t;
var currRev=1/currRate;
currRevFix=currRev.toFixed(5);
var currRevStatement= "1 " + curr +"= " + currRevFix + " EUR";
document.getElementById("rateRev").value=currRevSt atement;
};
  #2  
Old November 4th, 2008, 05:05 PM
Joe Fawcett
Guest
 
Posts: n/a

re: Parsing XML with namespace in IE. (getElementsByTagNameNS())


"Steve" <stephen.joung@googlemail.comwrote in message
news:8e1bb314-c61c-4fdc-8d9f-fd228715dc75@p31g2000prf.googlegroups.com...
Quote:
With the help of this newsgroup and Google I have got this code
working fully in Firefox and can alert the XML in IE but because IE
does not impliment the DOM "getElementsByTagNameNS()" function I
cannot read the individual rates from the Cube namespace.
>
Is there a wrapper or some other relatively simple method of getting
IE to do what in Firefox is straighforward?
>
You could always use XPath instead.
Instead of "Microsoft.XMLDOM" use "MSXML2.DomDocument.3.0".
The set the SelectionLanguage
doc1.setProperty("SelectionLanguage", "XPath")
then the namespace prefix:
doc1.setProperty("SelectionNamespaces", "xmlns:df = ''http://www.ecb.int/
vocabulary/2002-08-01/eurofxref'");

Then use XPath:
var cubes = doc1.selectNodes(<xpath expression here>);
If you show the XML I can tell you the XPath :)

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name


Closed Thread