Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Parsing an XML file with namespace

From: Steve <stephen.joung@----------.--->
To: NULL
Date: 9/23/2008 10:52:00 AM
On Sep 22, 12:12=A0pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> 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/eur=
ofxref/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
> =A0 =A0var doc =3D document.implementation.createDocument('', 'dummy', nu=
ll);
> =A0 =A0doc.onload =3D function ()
> =A0 =A0{
> =A0 =A0 =A0var cubes =3D
> doc.getElementsByTagNameNS('http://www.ecb.int/vocabulary/2002-08-01/euro=
fxref',
> 'Cube');
> =A0 =A0 =A0for (var i =3D 0, l =3D cubes.length; i < l; i++)
> =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 var currency =3D cubes[i].getAttribute('currency');
> =A0 =A0 =A0 =A0 var rate =3D cubes[i].getAttribute('rate');
> =A0 =A0 =A0 =A0 // now use rate/currency here
> =A0 =A0 =A0}
> =A0 =A0};
> =A0 =A0doc.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 =3D document.implementation.createDocument('', 'dummy', null);
> doc.onload =3D function ()
> {
> =A0 =A0 var xpathResult =3D doc.evaluate(
> =A0 =A0 =A0 '//ef:Cube[@currency]',
> =A0 =A0 =A0 doc,
> =A0 =A0 =A0 {lookupNamespaceURI : function (prefix) {
> =A0 =A0 =A0 =A0 =A0if (prefix =3D=3D=3D 'ef') return
> 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref';
> =A0 =A0 =A0 =A0}},
> =A0 =A0 =A0 XPathResult.ORDERED_NODE_ITERATOR_TYPE,
> =A0 =A0 =A0 null
> =A0 =A0 );
> =A0 =A0 var node;
> =A0 =A0 var table =3D document.createElement('table');
> =A0 =A0 var tbody =3D document.createElement('tbody');
> =A0 =A0 while ((node =3D xpathResult.iterateNext()) !=3D null)
> =A0 =A0 {
> =A0 =A0 =A0 var currency =3D node.getAttribute('currency');
> =A0 =A0 =A0 var rate =3D node.getAttribute('rate');
> =A0 =A0 =A0 var tr =3D document.createElement('tr');
> =A0 =A0 =A0 var td =3D document.createElement('td');
> =A0 =A0 =A0 td.appendChild(document.createTextNode(currency));
> =A0 =A0 =A0 tr.appendChild(td);
> =A0 =A0 =A0 td =3D document.createElement('td');
> =A0 =A0 =A0 td.appendChild(document.createTextNode(rate));
> =A0 =A0 =A0 tr.appendChild(td);
> =A0 =A0 =A0 tbody.appendChild(tr);
> =A0 =A0 }
> =A0 =A0 table.appendChild(tbody);
> =A0 =A0 document.body.appendChild(table);};
>
> doc.load('rates.xml');
>
> --
>
> =A0 =A0 =A0 =A0 Martin Honnen
> =A0 =A0 =A0 =A0http://JavaScript.FAQTs.com/

Thanks Martin,

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

Regards, Steve.


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent