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(); } } }把下面这些换上就行了,我测试了。 Element students = (Element) root.getChildren().get(0); List citys = ((Element)students.getChildren().get(0)).getChildren() ; System.out.println("城市\t最高气温\t最低气温\t风速"); for(int i=0;i<citys.size();i++){ Element element = (Element)citys.get(i);
weather.xml
最新推荐文章于 2019-04-10 10:08:25 发布