0

I am trying to parse this java XML http://www.bnr.ro/nbrfxrates.xml and i want to extract the rate of the chosen curency, this is what I have done so far and it is not working... Can you tell me what the the right way to get to the currency rate and print it out inside the switch? ps: the xml is loaded using:

public static void main(String[] args) throws SAXException, IOException  {
    URL oracle = new URL("http://www.bnr.ro/nbrfxrates.xml");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                            yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();





Scanner keyboard = new Scanner(System.in);
    System.out.println("What currency rate do you need?");
    String myString;
    myString = keyboard.nextLine();
    System.out.println("myint: " + myString);

    String ales = myString;

    switch(ales ){
        case "USD":
           final Pattern pattern = Pattern.compile("<Rate>(.+?)</Rate>");
           final Matcher matcher = pattern.matcher("<Rate></Rate>");
            matcher.find();
            System.out.println(matcher.group(1));

            break;


        default:
            System.out.println("We don't know");
3
  • you don't pass inputLine to the matcher. inputLine will contain only the last line from the url Commented Nov 8, 2015 at 12:35
  • Where are you setting "chosen"? Commented Nov 8, 2015 at 12:39
  • The correct way to parse xml is with an cal parser: see docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm Commented Nov 8, 2015 at 12:42

1 Answer 1

1

without XML parsing you could change your code to something like this - this will extract the correct currency based on regex alone:

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class currency {
public static void main(String[] args) throws Exception  {
    URL oracle = new URL("http://www.bnr.ro/nbrfxrates.xml");
    URLConnection yc = oracle.openConnection();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    BufferedReader in = new BufferedReader(new InputStreamReader(
                            yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) { 
        System.out.println(inputLine);
        pw.println(inputLine);
    }
    in.close();
    pw.close();
    sw.close();

    String data = sw.toString();

    Scanner keyboard = new Scanner(System.in);
    System.out.println("What currency rate do you need?");
    String myString;
    myString = keyboard.nextLine();
    keyboard.close();
    System.out.println("myint: " + myString);

    String ales = myString;

    Pattern pattern = Pattern.compile("<Rate currency=\""+ales+"\">(.+?)</Rate>");
    Matcher matcher = pattern.matcher(data);
    matcher.find();
    System.out.println(matcher.group(1));
    System.out.println(Double.valueOf(matcher.group(1)));
  }
}

Of cause keeping XML as string is not the best of ways to do this - as is parsing by regex.

But you could give it a try and optimize from there. Like: Putting the currencies into a map and then using this for further procesing:

Map<String,Double> currencies = new HashMap<String,Double>();
    Pattern pattern = Pattern.compile("<Rate currency=\"([^\"]{3})\">(.+?)</Rate>");
    Matcher matcher = pattern.matcher(data);
    int pos = 0;
    while(matcher.find(pos)) {
      System.out.println("Found: " + matcher.group(1) + ": " + Double.valueOf(matcher.group(2)));
      currencies.put(matcher.group(1), Double.valueOf(matcher.group(2)));
      pos = matcher.end();
    }

    Scanner keyboard = new Scanner(System.in);

    boolean ok = true;
    while(ok) {
      System.out.println("What currency rate do you need? (QUIT to quit)");
      String cur = keyboard.nextLine();
      System.out.println("Rate for " + cur + " is " + currencies.get(cur));
      if("QUIT".equalsIgnoreCase(cur)) {
        ok = false;
      }
    }    
    keyboard.close();
Sign up to request clarification or add additional context in comments.

2 Comments

Hello janschweizer, It works, It`s great thank you verry much for your help!
sorry, i tried voting it up and it told me i needed a 15 rep mininum, it looks like accepting does not have that limitation. Thank you again for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.