= #JxnPortable/docs/programmer_examples/demos/EuroParserURL.jxn
!
! retrieves currency rates as XML file from www.ecb.europa.eu and calls plot( time, rate )
!
url = @URL( "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml" )
! replace "eurofxref-hist-90d.xml" by "eurofxref-hist.xml" to see currency rates of the last 20 years
input = url.openStream()
! or after manual download of the xml file:
! input = @FileInputStream "eurofxref-hist-90d.xml"
!
builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder()
document = builder.parse( input )
nodes = document.getDocumentElement().getChildNodes().item(2).getChildNodes()
mxCurrency = nodes.item(0).getChildNodes().getLength() - 1
!
slider = @JxnSliderPanel( $this )
w = slider.add( "Currency", 5, 0, 100 )
currencyId = KmgXMLUtil.getAttributeValue( nodes.item(0).getChildNodes().item( limit( round w, 0, mxCurrency ) ), "currency" )
!
KmgXMLUtil.toString nodes.item(1)
#if eq( nodes.item(1).getNodeType(), org.w3c.dom.Node.ELEMENT_NODE )
dayNodes = nodes.item( { 0 : nodes.getLength() - 1 } )
#else  ! nodes.item(1) is empty text node => use every 2nd node
dayNodes = nodes.item( 2 { 0 : ( nodes.getLength() - 1 ) / 2 } )  ! this and the following use jxn auto array
#endif
time = KmgDateTimeConverter.parseDateTime( KmgXMLUtil.getAttributeValue( dayNodes, "time" ) )
iCurrency = KmgXMLUtil.findAttributeValue( dayNodes.getChildNodes(), "currency", currencyId )
! iCurrency contains the positions of the selected currency in the different day nodes
! - position in the day node may change because of new or abandoned currencies
rate = KmgStaticUtilities.parseDouble( KmgXMLUtil.getAttributeValue( dayNodes.getChildNodes().item( iCurrency ), "rate" ) )
!
pf = plot( time, rate, 4 ).setAutoScale( 2 ).setPlotFrameTitle( "1 EUR -> .. " + currencyId )