在SAX解析时发生了空指针异常
weather.xml<?xmlversion="1.0"encoding="utf-8"?><countrys><countrysnamename="china"><c...
weather.xml
<?xml version="1.0" encoding="utf-8"?>
<countrys>
<countrysname name="china">
<citys>
<city>
<name>秦皇岛</name>
<high>28</high>
<low>18</low>
<wind>三级</wind>
</city>
<city>
<name>天津</name>
<high>30</high>
<low>18</low>
<wind>二级</wind>
</city>
</citys>
</countrysname>
</countrys>
TestWeather.class
package com.view;
import java.io.*;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import java.util.List;
public class TestWeather {
public TestWeather() {
}
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("weather.xml");
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(fis);
Element root = doc.getRootElement();
List students = root.getChildren();
System.out.println("城市\t最高气温\t最低气温\t风速");
//在这附近有错误,可是不知道怎么改
for(int i=0;i<students.size();i++)
{
Element element = (Element)students.get(i);
String name = element.getChild("name").getText();
String high = element.getChild("high").getText();
String low = element.getChild("low").getText();
String wind = element.getChild("wind").getText();
System.out.println(name+"\t"+high+"\t"+low+"\t"+wind);
}
fis.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
} 展开
<?xml version="1.0" encoding="utf-8"?>
<countrys>
<countrysname name="china">
<citys>
<city>
<name>秦皇岛</name>
<high>28</high>
<low>18</low>
<wind>三级</wind>
</city>
<city>
<name>天津</name>
<high>30</high>
<low>18</low>
<wind>二级</wind>
</city>
</citys>
</countrysname>
</countrys>
TestWeather.class
package com.view;
import java.io.*;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import java.util.List;
public class TestWeather {
public TestWeather() {
}
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("weather.xml");
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(fis);
Element root = doc.getRootElement();
List students = root.getChildren();
System.out.println("城市\t最高气温\t最低气温\t风速");
//在这附近有错误,可是不知道怎么改
for(int i=0;i<students.size();i++)
{
Element element = (Element)students.get(i);
String name = element.getChild("name").getText();
String high = element.getChild("high").getText();
String low = element.getChild("low").getText();
String wind = element.getChild("wind").getText();
System.out.println(name+"\t"+high+"\t"+low+"\t"+wind);
}
fis.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询