從本質上講,我要帶一堆:添加正則表達式匹配HashMap和訪問它們
<Cube currency="USD" rate="1.2954"/>
並投入格式的一個HashMap:
String USD, String 1.2954
我有以下代碼:
Map<String, String> list = new HashMap<String, String>();
Pattern p = Pattern.compile("<Cube\\scurrency='(.*)'\\srate='(.*)'/>");
Matcher matcher = p.matcher(currency_source);
while (matcher.find()) {
list.put(matcher.group(1)); // PROBLEMATIC
}
我不知道如何正確地將貨幣和匯率添加到散列表。
1.你的源代碼看起來像XML,那麼爲什麼你不只是一個XML解析器,並將它們添加到你的地圖中? 2. HaspMap使用鍵/值,matcher()返回一個String。 – MemLeak