Hello! Please help. I need on the currency rate ( XML ) page, pull out the values of USD, EUR, RUB and write them into the corresponding variables. So far I have achieved such a picture (filtered out all unnecessary):
Char3 : AUD Rate : 825.9537 Char3 : AZM Rate : 1018.6058 Char3 : GBP Rate : 1237.8745 Char3 : BYR Rate : 0.0093 Char3 : DKK Rate : 142.7915 Char3 : USD Rate : 799.3000 Char3 : EUR Rate : 1065.0673 Char3 : ISK Rate : 6.1894 Char3 : KZT Rate : 5.3127 Char3 : CAD Rate : 797.2060 Char3 : LVL Rate : 1522.3946 Char3 : LTL Rate : 308.4648 Char3 : MDL Rate : 66.2698 Char3 : NOK Rate : 144.0351 Char3 : PLN Rate : 254.1563 Char3 : RUB Rate : 2.6543 Char3 : SGD Rate : 646.0043 Char3 : XDR Rate : 1220.1004 Char3 : TRL Rate : 451.8358 Char3 : TMM Rate : 280.4561 Char3 : HUF Rate : 36.4275 Char3 : UZS Rate : 0.3976 Char3 : CZK Rate : 41.9549 Char3 : SEK Rate : 125.9585 Char3 : CHF Rate : 865.6972 Char3 : CNY Rate : 128.2534 Char3 : JPY Rate : 85.8717
Here is my code:
public class TestSaxParser { public static void main(String argv[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean blchar = false; boolean bnrate = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("char3")) { blchar = true; } if (qName.equalsIgnoreCase("rate")) { bnrate = true; } } public void endElement(String uri, String localName, String qName) throws SAXException {} public void characters(char ch[], int start, int length) throws SAXException { if (blchar) { System.out.println("Char3 : " + new String(ch, start, length)); blchar = false; } if (bnrate) { System.out.println("Rate : " + new String(ch, start, length)); bnrate = false; } } }; saxParser.parse("http://bank-ua.com/export/currrate.xml", handler); } catch (Exception e) { e.printStackTrace(); } } }
Tell me how to save the values I need))