Connecting Tech Pros Worldwide Help | Site Map

Re: Parsing an XML file with namespace

Martin Honnen
Guest
 
Posts: n/a
#1: Sep 22 '08
Steve wrote:
Quote:
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
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/
Steve
Guest
 
Posts: n/a
#2: Sep 22 '08

re: Re: Parsing an XML file with namespace


On Sep 22, 12:12 pm, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Well saying you use JavaScript does not tell us much. Which application
(e.g. which browser) provides the host environment?
As javascript is client -side I can only assume that different
browsers will be used to view the page. Differences between ECMAScript
and JScript, W3C DOM and MS DOM etc. have to be dealt with within the
javascript coding. Therefore my javascript applications are tested on
all main browsers. You can visit some of them at
http://members.chello.at/stephen.joung/indexMaps.html
Quote:
>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.
I have downloaded the ECB XML file from the ECB page and uploaded it
to my Webserver using PHP/cURL and CRON. Therefore the XML file is on
the same server as the javascript and there is no problem with same
origin policy.
Quote:
Assuming you have a browser like Mozilla or Opera which support the W3C
DOM Level 2 Core you can use....
Thank you, I will try your suggestions tonight and let you know how I
get on.

Regards, Steve.
Steve
Guest
 
Posts: n/a
#3: Sep 23 '08

re: Re: Parsing an XML file with namespace


On Sep 22, 12:12*pm, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Steve wrote:
Quote:
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 fromhttp://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
>
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/
Thanks Martin,

your suggestions work and have given me some insight into XML.

Regards, Steve.
Steve
Guest
 
Posts: n/a
#4: Sep 23 '08

re: Re: Parsing an XML file with namespace


On Sep 22, 12:12*pm, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Steve wrote:
Quote:
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 fromhttp://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
>
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/
Thanks Martin,

your suggestions work and have given me some insight into XML.

Regards, Steve.
Closed Thread