简体   繁体   English

Java XML解析问题

[英]Java XML parsing issue

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? 我正在尝试解析此java XML http://www.bnr.ro/nbrfxrates.xml ,我想提取所选货币的汇率,这是我到目前为止所做的,并且无法正常工作...可以您告诉我什么正确的方法来获得货币汇率并在交换机内部打印出来? ps: the xml is loaded using: ps:使用以下命令加载xml:

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");

without XML parsing you could change your code to something like this - this will extract the correct currency based on regex alone: 如果没有XML解析,则可以将代码更改为以下内容-这将仅基于正则表达式提取正确的货币:

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. 将XML保留为字符串并不是实现此目的的最佳方法-正则表达式解析也是如此。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM