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.implementation.createDocument('', 'dummy', null);
doc.onload = function ()
{
var cubes =
doc.getElementsByTagNameNS('http://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.implementation.createDocument('', 'dummy', null);
doc.onload = function ()
{
var xpathResult = doc.evaluate(
'//ef:Cube[@currency]',
doc,
{lookupNamespaceURI : function (prefix) {
if (prefix === 'ef') return
'http://www.ecb.int/vocabulary/2002-08-01/eurofxref';
}},
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null
);
var node;
var table = document.createElement('table');
var tbody = document.createElement('tbody');
while ((node = xpathResult.iterateNext()) != null)
{
var currency = node.getAttribute('currency');
var rate = node.getAttribute('rate');
var tr = document.createElement('tr');
var td = document.createElement('td');
td.appendChild(document.createTextNode(currency));
tr.appendChild(td);
td = document.createElement('td');
td.appendChild(document.createTextNode(rate));
tr.appendChild(td);
tbody.appendChild(tr);
}
table.appendChild(tbody);
document.body.appendChild(table);
};
doc.load('rates.xml');
--
Martin Honnen
http://JavaScript.FAQTs.com/