【问题标题】:How to solve - The element type meta must be terminated by the matching end-tag </meta> [closed]如何解决 - 元素类型 meta 必须由匹配的结束标签终止 </meta> [关闭]
【发布时间】:2017-09-20 12:16:47
【问题描述】:

我尝试为天气应用程序编写代码,但出现此错误:

org.xml.sax.SAXParseException;行号:2;列号: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);
        }
    }
}

提前致谢。

【问题讨论】:

    标签: java xml weather


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2020-09-05
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多