当前位置: 首页 > 工具软件 > weather china > 使用案例 >

weather.xml

裴姚石
2023-12-01
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);
 类似资料:

相关阅读

相关文章

相关问答