我正在尝试解析这个java XML http://www.bnr.ro/nbrfxrates.xml,我想提取所选择的治愈率,这是我到目前为止所做的并且它不起作用...你能告诉我是什么正确的方法来获得货币汇率并在交换机内打印出来? ps:使用:
加载xmlpublic 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");
答案 0 :(得分:1)
如果没有XML解析,您可以将代码更改为这样的代码 - 这将仅基于正则表达式提取正确的货币:
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();
将XML保持为字符串的原因并不是最好的方法 - 就像正则表达式解析一样。
但是你可以尝试一下并从那里进行优化。比如:将货币放入地图,然后使用它进行进一步处理:
QRect rectangle=ui->w_mapa->rect();
QPixmap pixmap(rectangle.size());
ui->w_mapa->render(&pixmap, QPoint(), QRegion(rectangle));
QString nombre_fichero=QString("x%1.jpg").arg(tam_castillos);
pixmap.save(nombre_fichero);