我正在编写一个程序,该程序从http://www.tcmb.gov.tr/kurlar/today.xml获取每日汇率并将其保存到列表中。这是我的代码:
public class ParseTheXml {
private List<CurrencyPojo> currencyList;
public void setCurrencyList(List<CurrencyPojo> currencyList) {
this.currencyList = currencyList;
}
public List<CurrencyPojo> getCurrencyList() throws ParserConfigurationException {
currencyList = new ArrayList<CurrencyPojo>();
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL url = new URL("http://www.tcmb.gov.tr/kurlar/today.xml");
Document document = builder.parse(url.openStream());
NodeList nodeList = document.getElementsByTagName("Currency");
for (int i = 0; i < nodeList.getLength(); i++) { // döngü her currency türü için
Element element = (Element) nodeList.item(i);
CurrencyPojo currencyPojoObject = new CurrencyPojo();
currencyPojoObject.setCrossOrder(Integer.parseInt(element.getAttribute("CrossOrder")));
currencyPojoObject.setKod(element.getAttribute("Kod"));
currencyPojoObject.setCurrenyCode(element.getAttribute("CurrencyCode"));
currencyPojoObject.setUnit(Integer.parseInt(element.getElementsByTagName("Unit").item(0).getTextContent()));
currencyPojoObject.setIsim(element.getElementsByTagName("Isim").item(0).getTextContent());
currencyPojoObject.setCurrencyName(element.getElementsByTagName("CurrencyName").item(0).getTextContent());
currencyPojoObject.setForexBuying(Double.parseDouble(element.getElementsByTagName("ForexBuying").item(0).getTextContent()));
currencyPojoObject.setForexSelling(Double.parseDouble(element.getElementsByTagName("ForexSelling").item(0).getTextContent())); // Last 3 codes have Emppty String errors
currencyPojoObject.setBanknoteBuying(Double.parseDouble(element.getElementsByTagName("BanknoteBuying").item(0).getTextContent()));
currencyPojoObject.setBanknoteSelling(Double.parseDouble(element.getElementsByTagName("BanknoteSelling").item(0).getTextContent()));
currencyList.add(currencyPojoObject);
}
} catch (Exception e) {
e.printStackTrace();
}
return currencyList;
}
关于获得ForexSelling,BanknoteBuying和Banknote Selling价值的部分,我得到了
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at javaxmlparseandshow.ParseTheXml.getCurrencyList(ParseTheXml.java:55)
at javaxmlparseandshow.ParseTheXml.main(ParseTheXml.java:75)
有人可以帮我解决这些问题吗?预先感谢。
答案 0 :(得分:0)
据错误堆栈跟踪显示, element.getElementsByTagName(“ ForexSelling”)字段没有数据。解析器尝试将空数据转换为字符串并引发错误。 查看xml,您必须看到空白字段。 作为解决方案,我认为您需要首先检查数据是否为空:
答案 1 :(得分:0)
您尝试了以下方法吗
String forexStr = element.getElementsByTagName("ForexSelling").item(0).getTextContent();
if( /* forexStr conforms to your criteria like, for example: !forexStr.equals("")*/){
currencyPojoObject.setForexSelling(Double.parseDouble(forexStr);
}
?
答案 2 :(得分:0)
或者,您可以将0设置为空xml字符串
private double parseDouble(String val){
if(val== null || val.isEmpty())
return 0.0;
else
return Double.parseDouble(val);
}
EX:
currencyPojoObject.setForexSelling(parseDouble(element.getElementsByTagName("ForexSelling").item(0).getTextContent()));