Re: Parsing an XML file with namespace

Discussion in 'XML' started by Martin Honnen, Sep 22, 2008.

  1. Steve wrote:
    > 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.getAttribute('currency');
    var rate = cubes.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/
    Martin Honnen, Sep 22, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    559
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Peter Flynn
    Replies:
    3
    Views:
    1,745
    Peter Flynn
    Sep 22, 2008
  3. Joe Fawcett
    Replies:
    0
    Views:
    928
    Joe Fawcett
    Nov 4, 2008
  4. John Levine
    Replies:
    0
    Views:
    718
    John Levine
    Feb 2, 2012
  5. Erik Wasser
    Replies:
    5
    Views:
    437
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page