Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Parsing an XML file with namespace [Thread Next] Re: Parsing an XML file with namespaceTo: 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.
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
