Steve wrote:
Well saying you use JavaScript does not tell us much. Which application
(e.g. which browser) provides the host environment? If it is a browser
which provides the host environment, is your script located on the same
server as the XML? Otherwise you will run into same origin policy
restrictions and might not be able to load the XML at all.
Assuming you have a browser like Mozilla or Opera which support the W3C
DOM Level 2 Core you can use
var doc = document.implem entation.create Document('', 'dummy', null);
doc.onload = function ()
{
var cubes =
doc.getElements ByTagNameNS('ht tp://www.ecb.int/vocabulary/2002-08-01/eurofxref',
'Cube');
for (var i = 0, l = cubes.length; i < l; i++)
{
var currency = cubes[i].getAttribute(' currency');
var rate = cubes[i].getAttribute(' rate');
// now use rate/currency here
}
};
doc.load('rates .xml');
As an altenative approach you might want to try to use the W3C DOM Level
3 XPath API as that gives you more control with the power of XPath:
var doc = document.implem entation.create Document('', 'dummy', null);
doc.onload = function ()
{
var xpathResult = doc.evaluate(
'//ef:Cube[@currency]',
doc,
{lookupNamespac eURI : function (prefix) {
if (prefix === 'ef') return
'http://www.ecb.int/vocabulary/2002-08-01/eurofxref';
}},
XPathResult.ORD ERED_NODE_ITERA TOR_TYPE,
null
);
var node;
var table = document.create Element('table' );
var tbody = document.create Element('tbody' );
while ((node = xpathResult.ite rateNext()) != null)
{
var currency = node.getAttribu te('currency');
var rate = node.getAttribu te('rate');
var tr = document.create Element('tr');
var td = document.create Element('td');
td.appendChild( document.create TextNode(curren cy));
tr.appendChild( td);
td = document.create Element('td');
td.appendChild( document.create TextNode(rate)) ;
tr.appendChild( td);
tbody.appendChi ld(tr);
}
table.appendChi ld(tbody);
document.body.a ppendChild(tabl e);
};
doc.load('rates .xml');
--
Martin Honnen
I am new to XML but have managed to parse most of the XML files I have
come across within my javascript scripts. Until now. I am trying to
parse the currency file available free from http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
come across within my javascript scripts. Until now. I am trying to
parse the currency file available free from http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
(e.g. which browser) provides the host environment? If it is a browser
which provides the host environment, is your script located on the same
server as the XML? Otherwise you will run into same origin policy
restrictions and might not be able to load the XML at all.
Assuming you have a browser like Mozilla or Opera which support the W3C
DOM Level 2 Core you can use
var doc = document.implem entation.create Document('', 'dummy', null);
doc.onload = function ()
{
var cubes =
doc.getElements ByTagNameNS('ht tp://www.ecb.int/vocabulary/2002-08-01/eurofxref',
'Cube');
for (var i = 0, l = cubes.length; i < l; i++)
{
var currency = cubes[i].getAttribute(' currency');
var rate = cubes[i].getAttribute(' rate');
// now use rate/currency here
}
};
doc.load('rates .xml');
As an altenative approach you might want to try to use the W3C DOM Level
3 XPath API as that gives you more control with the power of XPath:
var doc = document.implem entation.create Document('', 'dummy', null);
doc.onload = function ()
{
var xpathResult = doc.evaluate(
'//ef:Cube[@currency]',
doc,
{lookupNamespac eURI : function (prefix) {
if (prefix === 'ef') return
'http://www.ecb.int/vocabulary/2002-08-01/eurofxref';
}},
XPathResult.ORD ERED_NODE_ITERA TOR_TYPE,
null
);
var node;
var table = document.create Element('table' );
var tbody = document.create Element('tbody' );
while ((node = xpathResult.ite rateNext()) != null)
{
var currency = node.getAttribu te('currency');
var rate = node.getAttribu te('rate');
var tr = document.create Element('tr');
var td = document.create Element('td');
td.appendChild( document.create TextNode(curren cy));
tr.appendChild( td);
td = document.create Element('td');
td.appendChild( document.create TextNode(rate)) ;
tr.appendChild( td);
tbody.appendChi ld(tr);
}
table.appendChi ld(tbody);
document.body.a ppendChild(tabl e);
};
doc.load('rates .xml');
--
Martin Honnen
Comment