2016-07-31 124 views
0

我的實施SAXParser類使用URL地址來處理XML數據不會返回結果。該班級使用額外的貨幣類別,這些貨幣類別又將currIdrate兩個變量存儲在setter/getters中。當我運行我的課時,沒有出現在Java控制檯中。這裏是代碼:SAXParser不從URL中檢索XML數據

public class MySAXParser extends DefaultHandler { 

    private static List<Currencies> currencies = new ArrayList<Currencies>(); 
    private static Currencies curr = null; 
    private static String text = null; 

    public static void main(String[] args) { 

     String url = "http://nbt.tj/en/kurs/export_xml.php?date=2016-08-01&export=xmlout"; 

     try { 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      MySAXParser handler = new MySAXParser(); 
      URL uri = new URL(url); 
      sp.parse(new InputSource(uri.openStream()), handler); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     for (Currencies curr : currencies) { 
      System.out.println(curr.toString()); 
     } 

    } 

    public void startElement (String s, String s1, String elementName, Attributes atts) throws SAXException { 
     if (elementName.equalsIgnoreCase("valute")) { 
      curr = new Currencies(); 
      curr.setCurrId(atts.getValue("id")); 
     } 
    } 

    public void endElement (String s, String s1, String element) throws SAXException { 
     if (element.equals("valute")) { 
      currencies.add(curr); 
     } 
     if (element.equalsIgnoreCase("value")) { 
      curr.setRate(Double.parseDouble(text)); 
     } 
    } 

    @Override 
    public void characters (char[] ch, int start, int length) throws SAXException { 
     text = String.copyValueOf(ch, start, length).trim(); 
    } 

} 

那麼,我錯過了什麼或做錯了什麼?任何幫助,將不勝感激。

+0

是否比較's1.equalsIgnoreCase '而不是第三個參數的幫助? –

+0

@MartinHonnen,使用第二個參數's1'也沒有結果。 – Dozent

+0

據我所知,SAX並不保證將文本節點報告爲單個字符事件,因此您還需要改進該代碼。但是,正如你所說,根本沒有得到任何結果,我只是通過閱讀代碼我不確定什麼是錯的。如果沒有人看到問題,用調試器逐個檢查出現了什麼問題。 –

回答

1

這裏是我的嘗試是與Java 1.8工作正常:

import java.net.URL; 
import java.util.ArrayList; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.Attributes; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 


public class MySAXParser extends DefaultHandler { 

    private List<Currency> currencies = new ArrayList<>(); 
    private Currency curr = null; 
    private StringBuilder sb; 

    public static void main(String[] args) { 

     String url = "http://nbt.tj/en/kurs/export_xml.php?date=2016-08-01&export=xmlout"; 

     try { 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      spf.setNamespaceAware(true); 

      SAXParser sp = spf.newSAXParser(); 
      MySAXParser handler = new MySAXParser(); 

      sp.parse(new InputSource(url), handler); 

      for (Currency curr : handler.getCurrencies()) { 
       System.out.println(curr.toString()); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 

    public List<Currency> getCurrencies() { 
     return currencies; 
    } 

    @Override 
    public void startElement(String s, String localName, String elementName, Attributes atts) throws SAXException { 
     if (elementName.equalsIgnoreCase("valute")) { 
      curr = new Currency(); 
      currencies.add(curr); 
      curr.setCurrId(atts.getValue("ID")); 
     } else if (elementName.equalsIgnoreCase("value") || elementName.equalsIgnoreCase("CharCode")) { 
      sb = new StringBuilder(); 
     } 
    } 

    @Override 
    public void endElement(String s, String localName, String elementName) throws SAXException { 
     if (elementName.equalsIgnoreCase("value")) { 
      curr.setRate(Double.parseDouble(sb.toString())); 
      sb = null; 
     } 
     else if (elementName.equalsIgnoreCase("CharCode")) { 
      curr.setCharCode(sb.toString()); 
      sb = null; 
     } 
    } 

    @Override 
    public void characters(char[] ch, int start, int length) throws SAXException { 
     if (sb != null) { 
      sb.append(ch, start, length); 
     } 
    } 

} 

類是

public class Currency { 

    private String currId; 

    /** 
    * Get the value of currId 
    * 
    * @return the value of currId 
    */ 
    public String getCurrId() { 
     return currId; 
    } 

    /** 
    * Set the value of currId 
    * 
    * @param currId new value of currId 
    */ 
    public void setCurrId(String currId) { 
     this.currId = currId; 
    } 

     private double rate; 

    /** 
    * Get the value of rate 
    * 
    * @return the value of rate 
    */ 
    public double getRate() { 
     return rate; 
    } 

    /** 
    * Set the value of rate 
    * 
    * @param rate new value of rate 
    */ 
    public void setRate(double rate) { 
     this.rate = rate; 
    } 

    private String charCode; 

    /** 
    * Get the value of charCode 
    * 
    * @return the value of charCode 
    */ 
    public String getCharCode() { 
     return charCode; 
    } 

    /** 
    * Set the value of charCode 
    * 
    * @param charCode new value of charCode 
    */ 
    public void setCharCode(String charCode) { 
     this.charCode = charCode; 
    } 

    @Override 
    public String toString() { 
     return "Currency{" + "currId=" + currId + ", rate=" + rate + ", charCode=" + charCode + '}'; 
    } 

} 

示例輸出我得到的是

Currency{currId=840, rate=7.8683, charCode=USD} 
Currency{currId=978, rate=8.7448, charCode=EUR} 
Currency{currId=960, rate=10.9395, charCode=XDR} 
Currency{currId=156, rate=1.1828, charCode=CNY} 
Currency{currId=756, rate=8.075, charCode=CHF} 
Currency{currId=810, rate=0.1146, charCode=RUB} 
Currency{currId=860, rate=0.2655, charCode=UZS} 
Currency{currId=417, rate=1.1643, charCode=KGS} 
Currency{currId=398, rate=0.2234, charCode=KZT} 
Currency{currId=933, rate=3.9424, charCode=BYR} 
Currency{currId=364, rate=0.2617, charCode=IRR} 
Currency{currId=971, rate=1.139, charCode=AFN} 
Currency{currId=586, rate=0.7504, charCode=PKR} 
Currency{currId=949, rate=2.6076, charCode=TRY} 
Currency{currId=934, rate=2.2481, charCode=TMT} 
Currency{currId=826, rate=10.3618, charCode=GBP} 
Currency{currId=36, rate=5.9162, charCode=AUD} 
Currency{currId=208, rate=1.1755, charCode=DKK} 
Currency{currId=352, rate=0.659, charCode=ISK} 
Currency{currId=124, rate=5.9699, charCode=CAD} 
Currency{currId=414, rate=26.004, charCode=KWD} 
Currency{currId=578, rate=0.9193, charCode=NOK} 
Currency{currId=702, rate=5.8215, charCode=SGD} 
Currency{currId=752, rate=0.9136, charCode=SEK} 
Currency{currId=392, rate=0.761, charCode=JPY} 
Currency{currId=944, rate=4.9639, charCode=AZN} 
Currency{currId=51, rate=1.6516, charCode=AMD} 
Currency{currId=981, rate=3.3539, charCode=GEL} 
Currency{currId=498, rate=0.3979, charCode=MDL} 
Currency{currId=980, rate=0.317, charCode=UAH} 
Currency{currId=784, rate=2.1421, charCode=AED} 
Currency{currId=682, rate=2.0979, charCode=SAR} 
Currency{currId=356, rate=1.175, charCode=INR} 
Currency{currId=985, rate=2.0039, charCode=PLN} 
Currency{currId=458, rate=1.9313, charCode=MYR} 
Currency{currId=764, rate=0.2258, charCode=THB} 
+0

感謝您的提示。如果使用's1'參數並將SAXParserFactory設置爲名稱空間,也不會產生任何結果。 – Dozent

+0

@Dozent,現在我已經找到了啓動Java IDE並測試該代碼的時間,似乎我之前關於第二個參數的提示沒有幫助,因爲如果名稱空間未啓用,它也是空的,因此上面我已經使用了第三個參數在原始代碼片段中。 –

+0

感謝您的回答,它現在有效。然而,我沒有明白爲什麼它使用「Valute,Value,ID」而不是'valute,value,id'。你知道爲什麼嗎? – Dozent