如何解决 - 元素类型元必须由匹配的结束标记终止

时间:2017-09-20 12:16:48

标签: java xml weather

我试图为天气应用程序编写代码,我有这个错误:

  

org.xml.sax.SAXParseException; lineNumber:2; columnNumber:27;元素类型“meta”必须由匹配的结束标记“”终止。   在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)   在main.Weather.GoogleWeather.getWeather(GoogleWeather.java:38)   在main.Main.main(Main.java:12)

我不知道我是否正确设置了一切

我的代码:

public class GoogleWeather extends Weather{
    private final String googleWeatherURL = "http://www.google.com/ig/api?weather=";
    public GoogleWeather(String city, String locationPoints) throws IOException, SAXException {
        super(city, locationPoints);
    }

    @Override
    public void getWeather() {
        String location = "Warsaw, PL";
        String link = googleWeatherURL + location;
        link = link.replace(" ", "%20");

        try {
            URL urlObject = new URL(link);
            InputStream in = urlObject.openStream();
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();

            XMLHandler xmlHandler = new XMLHandler();
            xmlReader.setContentHandler((ContentHandler) xmlHandler);

            InputSource inSource = new InputSource(in);

            xmlReader.parse(inSource);

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        }

public class XMLHandler extends DefaultHandler {
    private ArrayList<Integer> night = new ArrayList<Integer>();
    private ArrayList<Integer> day = new ArrayList<Integer>();
    private ArrayList<String> days = new ArrayList<String>();
    private ArrayList<String> conditions = new ArrayList<String>();


    public XMLHandler() {
        super();
    }

    @Override
    public void startDocument() {
        System.out.println("Start document");
    }


    @Override
    public void endDocument() {
        System.out.println("End document");
    }


    @Override
    public void startElement(String uri, String name, String qName, Attributes atts) {
        if (qName.compareTo("day_of_week") == 0) {
            String day = atts.getValue(0);
            System.out.println("Day: " + day+ " ; ");
            this.days.add(day);
        }
        if (qName.compareToIgnoreCase("low") == 0) {
            int night = Integer.parseInt(atts.getValue(0));
            System.out.print("Low: " + night + " ; ");
            this.night.add(night);
        }
        if (qName.compareToIgnoreCase("high") == 0) {
            int high = Integer.parseInt(atts.getValue(0));
            System.out.print("High: " + high + " ; ");
            this.day.add(high);
        }
        if (qName.compareToIgnoreCase("condition data") == 0) {
            String conditions = atts.getValue(0);
            System.out.print("Conditions: " + conditions + " ; ");
            this.conditions.add(conditions);
        }
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

在开始通过XML解析器推送之前,您是否尝试过此URL在请求时响应的内容?

您尝试使用的API已停用多年,Google只会为您提供301并提供HTML网页,以防来电者没有意识到它刚刚收到301。

此服务不再存在,您无法使用它,并且您尝试获取的URL不会为您提供XML,因此XML解析器为您提供错误的原因。