weather.xml

  
  
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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值