這裏是我的嘗試是與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}
是否比較's1.equalsIgnoreCase '而不是第三個參數的幫助? –
@MartinHonnen,使用第二個參數's1'也沒有結果。 – Dozent
據我所知,SAX並不保證將文本節點報告爲單個字符事件,因此您還需要改進該代碼。但是,正如你所說,根本沒有得到任何結果,我只是通過閱讀代碼我不確定什麼是錯的。如果沒有人看到問題,用調試器逐個檢查出現了什麼問題。 –