SAXParser不会从URL检索XML数据

时间:2016-07-31 16:43:11

标签: java xml url xml-parsing saxparser

我实施的 SAXParser 类使用 URL地址来处理XML数据,但不会返回结果。该类使用额外的Currency类,而后者又将两个变量currIdrate存储为setter / getters。当我运行我的课时,没有任何东西出现在java控制台中。这是代码:

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();
    }

}

那么,我错过了什么或做错了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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}