Re: Parsing an XML file with namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Honnen

    Re: Parsing an XML file with namespace

    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.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

  • Steve

    #2
    Re: Parsing an XML file with namespace

    On Sep 22, 12:12 pm, Martin Honnen <mahotr...@yaho o.dewrote:
    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

    >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.
    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.

    Comment

    • Steve

      #3
      Re: Parsing an XML file with namespace

      On Sep 22, 12:12 pm, Martin Honnen <mahotr...@yaho o.dewrote:
      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 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.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
             http://JavaScript.FAQTs.com/
      Thanks Martin,

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

      Regards, Steve.

      Comment

      • Steve

        #4
        Re: Parsing an XML file with namespace

        On Sep 22, 12:12 pm, Martin Honnen <mahotr...@yaho o.dewrote:
        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 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.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
               http://JavaScript.FAQTs.com/
        Thanks Martin,

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

        Regards, Steve.

        Comment

        Working...