【发布时间】: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);
}
}
}
提前致谢。
【问题讨论】: