栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中提取XML文件时出现问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中提取XML文件时出现问题

这对我来说非常有效:

package weather;import org.dom4j.document;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;public class GoogleWeather{    public static void main(String[] args)    {        for (String userZip : args)        { BufferedReader br = null; try {     String link = "http://www.google.com/ig/api?weather=" + userZip;     System.out.println(link);     URL googleWeatherService = new URL(link);     br = new BufferedReader(new InputStreamReader(googleWeatherService.openStream()));     SAXReader reader = new SAXReader();     document document = reader.read(googleWeatherService);     OutputFormat format = OutputFormat.createPrettyPrint();     XMLWriter writer = new XMLWriter(System.out, format);     writer.write(document); } catch (Exception e) {     e.printStackTrace(); } finally {     close(br); }        }    }    private static void close(BufferedReader br)    {        try        { if (br != null) {     br.close(); }        }        catch (Exception e)        { e.printStackTrace();        }    }}

这是它带来的结果:

<?xml version="1.0" encoding="UTF-8"?><xml_api_reply version="1">  <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">    <forecast_information>      <city data="Hebron, CT"/>      <postal_pre data="06248"/>      <latitude_e6 data=""/>      <longitude_e6 data=""/>      <forecast_date data="2011-02-12"/>      <current_date_time data="2011-02-13 03:00:47 +0000"/>      <unit_system data="US"/>    </forecast_information>    <current_conditions>      <condition data="Partly Cloudy"/>      <temp_f data="28"/>      <temp_c data="-2"/>      <humidity data="Humidity: 45%"/>      <icon data="/ig/images/weather/partly_cloudy.gif"/>      <wind_condition data="Wind: NW at 14 mph"/>    </current_conditions>    <forecast_conditions>      <day_of_week data="Sat"/>      <low data="16"/>      <high data="36"/>      <icon data="/ig/images/weather/partly_cloudy.gif"/>      <condition data="Partly Cloudy"/>    </forecast_conditions>    <forecast_conditions>      <day_of_week data="Sun"/>      <low data="30"/>      <high data="38"/>      <icon data="/ig/images/weather/snow.gif"/>      <condition data="Snow Showers"/>    </forecast_conditions>    <forecast_conditions>      <day_of_week data="Mon"/>      <low data="23"/>      <high data="46"/>      <icon data="/ig/images/weather/cloudy.gif"/>      <condition data="Cloudy"/>    </forecast_conditions>    <forecast_conditions>      <day_of_week data="Tue"/>      <low data="12"/>      <high data="29"/>      <icon data="/ig/images/weather/cloudy.gif"/>      <condition data="Windy"/>    </forecast_conditions>  </weather></xml_api_reply>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号