- 浏览: 1367997 次
- 性别:
- 来自: 江西
文章分类
- 全部博客 (287)
- oracle (30)
- Java (73)
- MySQL (16)
- Myeclipse/eclipse (12)
- javascript (15)
- JSP/Servlet (7)
- maven (14)
- AJAX (2)
- JQuery (9)
- tomcat (8)
- spring (21)
- Linux (28)
- PHP (9)
- UI (1)
- 编程错误及处理 (38)
- 多线程 (18)
- hibernate (10)
- Web Service (3)
- struts2 (6)
- log4j (3)
- SVN (4)
- DWR (1)
- lucene (1)
- 正则表达式 (4)
- jstl (2)
- SSL (3)
- POI (1)
- 网络编程 (1)
- 算法 (2)
- xml (4)
- 加密解密 (1)
- IO (7)
- jetty (2)
- 存储过程 (1)
- SQL Server (1)
- MongoDB (1)
- mybatis (1)
- ETL (1)
- Zookeeper (1)
- Hadoop (5)
- Redis (1)
- spring cloud (1)
最新评论
-
ron.luo:
牛逼,正解!
maven设定项目编码 -
lichaoqun:
java.sql.SQLException: Can't call commit when autocommit=true -
Xujian0000abcd:
Thanks...
Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the clas -
renyuan2ni:
[i][b][u]引用[list]
[*][img][flas ...
Manual close is not allowed over a Spring managed SqlSession -
851228082:
宋建勇 写道851228082 写道<!-- 文件拷贝时 ...
maven设定项目编码
HandlerXML.java--处理xml
ParsersXML.java--显示处理结果
执行效果如下:
**********预测数据**********
预测数据 [week=周四, hight=33, low=24, icon=/ig/images/weather/thunderstorm.gif, condition=雷阵雨]
预测数据 [week=周五, hight=32, low=24, icon=/ig/images/weather/chance_of_storm.gif, condition=可能有暴风雨]
预测数据 [week=周六, hight=31, low=24, icon=/ig/images/weather/chance_of_storm.gif, condition=可能有暴风雨]
预测数据 [week=周日, hight=31, low=24, icon=/ig/images/weather/chance_of_storm.gif, condition=可能有暴风雨]
**********预报信息**********
2012-06-07 17:00:00 +0000
2012-06-07
Ganzhou, Jiangxi
**********当天数据**********
/ig/images/weather/mostly_cloudy.gif
湿度: 60%
88
风向: 西南、风速:3 米/秒
多云
31
ForecastCondition.java--对象
所需jar包可从附件下载
引自:http://www.2cto.com/kf/201204/127466.html
package cn.com.songjy.xml; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class HandlerXML extends DefaultHandler { List<ForecastCondition> forecast_conditions = new ArrayList<ForecastCondition>(); ForecastCondition forecast_condition = new ForecastCondition(); Map<String, Object> forecast_information = new HashMap<String, Object>(); Map<String, Object> current_condition = new HashMap<String, Object>(); boolean current_condition_flag = false; boolean forecast_information_flag = false; boolean forecast_conditions_flag = false; public List<ForecastCondition> getForecastConditions() { return forecast_conditions; } public Map<String, Object> getForecastInformation(){ return forecast_information; } public Map<String, Object> getCurrentConditions(){ return current_condition; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("forecast_information".equals(qName)) forecast_information_flag = true; if(forecast_information_flag && "city".equals(qName)) forecast_information.put("city", attributes.getValue("data")); if(forecast_information_flag && "forecast_date".equals(qName)) forecast_information.put("forecast_date", attributes.getValue("data")); if(forecast_information_flag && "current_date_time".equals(qName)) forecast_information.put("current_date_time", attributes.getValue("data")); if("current_conditions".equals(qName)) current_condition_flag = true; if(current_condition_flag && "condition".equals(qName)) current_condition.put("condition", attributes.getValue("data")); if(current_condition_flag && "temp_f".equals(qName)) current_condition.put("temp_f", attributes.getValue("data")); if(current_condition_flag && "temp_c".equals(qName)) current_condition.put("temp_c", attributes.getValue("data")); if(current_condition_flag && "humidity".equals(qName)) current_condition.put("humidity", attributes.getValue("data")); if(current_condition_flag && "icon".equals(qName)) current_condition.put("icon", attributes.getValue("data")); if(current_condition_flag && "wind_condition".equals(qName)) current_condition.put("wind_condition", attributes.getValue("data")); if("forecast_conditions".equals(qName)) forecast_conditions_flag = true; if (forecast_conditions_flag && "day_of_week".equals(qName)) { String week = attributes.getValue("data"); forecast_condition = new ForecastCondition();//不可省略,否则将永远是最后一条记录 forecast_condition.setWeek(week); } if (forecast_conditions_flag && "low".equals(qName)) { int low = Integer.parseInt(attributes.getValue("data")); forecast_condition.setLow(low); } if (forecast_conditions_flag && "high".equals(qName)) { int hight = Integer.parseInt(attributes.getValue("data")); forecast_condition.setHight(hight); } if (forecast_conditions_flag && "icon".equals(qName)) { String icon = attributes.getValue("data"); forecast_condition.setIcon(icon); } if (forecast_conditions_flag && "condition".equals(qName)) { String condition = attributes.getValue("data"); forecast_condition.setCondition(condition); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if("current_conditions".equals(qName)) current_condition_flag = false; if("forecast_information".equals(qName)) forecast_information_flag = false; if("forecast_conditions".equals(qName)) forecast_conditions_flag = false; if ("forecast_conditions".equals(qName)) { forecast_conditions.add(forecast_condition); } } }
ParsersXML.java--显示处理结果
package cn.com.songjy.xml; import java.io.IOException; import java.io.StringReader; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class ParsersXML { public static void main(String[] args) { Map<String, Object> map = readXml(); List<ForecastCondition> forecastConditions = (List<ForecastCondition>) map.get("forecastConditions"); Map<String, Object> forecast_information = (Map<String, Object>) map.get("forecast_information"); Map<String, Object> current_condition = (Map<String, Object>) map.get("current_condition"); System.out.println("**********预测数据**********"); for (ForecastCondition weather : forecastConditions) { System.out.println(weather.toString()); } System.out.println("**********预报信息**********"); for (String key : forecast_information.keySet()) { System.out.println(forecast_information.get(key).toString()); } System.out.println("**********当天数据**********"); for (String key : current_condition.keySet()) { System.out.println(current_condition.get(key).toString()); } } public static Map<String, Object> readXml() { Map<String, Object> map = new HashMap<String, Object>(); SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser parser = factory.newSAXParser(); InputSource inputSource = new InputSource(new StringReader(getWeatherAsString())); HandlerXML handlerXML = new HandlerXML(); try { parser.parse(inputSource, handlerXML); map.put("forecastConditions", handlerXML.getForecastConditions()); map.put("forecast_information", handlerXML.getForecastInformation()); map.put("current_condition", handlerXML.getCurrentConditions()); return map; } catch (IOException e) { e.printStackTrace(); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return null; } //调用公开API获取XML数据 public static String getWeatherAsString() { String strUrl = "http://www.google.com/ig/api?hl=zh-cn&weather=Ganzhou,Jiangxi"; HttpGet getRequest = new HttpGet(strUrl); DefaultHttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(getRequest); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
执行效果如下:
**********预测数据**********
预测数据 [week=周四, hight=33, low=24, icon=/ig/images/weather/thunderstorm.gif, condition=雷阵雨]
预测数据 [week=周五, hight=32, low=24, icon=/ig/images/weather/chance_of_storm.gif, condition=可能有暴风雨]
预测数据 [week=周六, hight=31, low=24, icon=/ig/images/weather/chance_of_storm.gif, condition=可能有暴风雨]
预测数据 [week=周日, hight=31, low=24, icon=/ig/images/weather/chance_of_storm.gif, condition=可能有暴风雨]
**********预报信息**********
2012-06-07 17:00:00 +0000
2012-06-07
Ganzhou, Jiangxi
**********当天数据**********
/ig/images/weather/mostly_cloudy.gif
湿度: 60%
88
风向: 西南、风速:3 米/秒
多云
31
ForecastCondition.java--对象
package cn.com.songjy.xml; public class ForecastCondition { private String week; private int low; private int hight; private String icon; private String condition; @Override public String toString() { return "预测数据 [week=" + week + ", hight=" + hight + ", low=" + low + ", icon=" + icon + ", condition=" + condition + "]"; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public int getLow() { return low; } public void setLow(int low) { this.low = low; } public int getHight() { return hight; } public void setHight(int hight) { this.hight = hight; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getCondition() { return condition; } public void setCondition(String condition) { this.condition = condition; } }
所需jar包可从附件下载
引自:http://www.2cto.com/kf/201204/127466.html
- commons-codec-1.6.jar (227.3 KB)
- 下载次数: 3
- commons-logging-1.1.1.jar (59.3 KB)
- 下载次数: 1
- httpclient-4.2.jar (414.7 KB)
- 下载次数: 2
- httpcore-4.2.jar (218 KB)
- 下载次数: 2
发表评论
-
java.io.NotSerializableException: java.util.ArrayList$SubList
2016-03-15 11:36 2802错误信息: 原代码: if (null != dto ... -
java.lang.NoSuchMethodError
2015-10-30 19:50 1100在执行命令 java -Djava.ext.dirs=./de ... -
OutOfMemoryError
2015-01-07 10:49 647错误信息: 执行命令(修改前): java -Djava. ... -
自定义ClassLoader
2014-12-14 17:13 864一、自定义类加载器代码: package foo; im ... -
URL传递中文参数乱码问题
2014-03-14 12:55 1116项目特殊要求,form表单使用get方式提交,由此出现了中文乱 ... -
Java使用正则进行奇偶替换
2013-12-30 11:20 1563/** * 将位于偶数位的数字1替换为2 */ ... -
Java调用ping命令
2013-11-26 10:26 3643package cn.com.songjy; impor ... -
Linux开机启动Java程序
2013-11-25 15:26 3541一、编写启动Java程序shell脚本(smssend.sh) ... -
单例模式
2013-11-05 09:58 761Java之美[从菜鸟到高手演变]之设计模式 Java开发中 ... -
判定2个对象相等之重写equals方法
2013-10-18 11:03 1061package cn.com.songjy.test; ... -
SAX解析XML文件示例
2013-09-13 17:32 1015package cn.com.songjy.test.xml; ... -
二分查找算法(Binary Search)
2013-09-13 17:01 2378项目中遇到需要从数组中查找数据,但是算法很多,于是根据项目需求 ... -
Iterate over rows and cells(EXCEL文件的读取)
2013-09-13 14:13 4066package cn.com.songjy.test.exce ... -
jdk1.5的Exchanger同步工具
2013-08-27 14:04 663package cn.com.songjy.test.sock ... -
jdk1.5的CountDownLatch同步工具
2013-08-27 13:37 769package cn.com.songjy.test.sock ... -
jdk1.5的Semaphere同步工具
2013-08-23 14:20 1064package cn.com.songjy.test.so ... -
jdk1.5条件阻塞Condition的应用
2013-08-23 09:42 1333package cn.com.songjy.test.sock ... -
jdk1.5读写锁技术的妙用
2013-08-19 22:00 1366/** * ReadWriteLockTest.java ... -
jdk1.5的线程锁技术
2013-08-19 20:25 941/** * LockTest.java * cn.co ... -
Callable与Future的应用
2013-08-19 20:03 1039/** * CallableAndFuture.java ...
相关推荐
SAX解析XML文件的实例。一个项目同时用dom解析和sax解析xml文件貌似会报错,项目框架建一直是用sax和dom4j解析xml文件的。当我用dom解析xml文件。导入包后就报错识别不了xml文件的编码格式。于是做了一个sax解析xml...
Sax解析XML文件解
Java SAX解析Xml Demo(详细注释)
Servlet利用SAX解析XML文档,JAX,JSWDP,Servlet利用SAX解析XML文档
Sax解析xml文件详细例子,解析复杂节点的xml,把内容显示到view中,例子简单完整,易用
Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有...
下面是一个SAX解析XML的示例(有点长,因为详细注解了SAX事件处理的所有方法),SAX API中主要有四种处理事件的接口,它们分别是ContentHandler,DTDHandler, EntityResolver 和 ErrorHandler 。下面的例子可能...
使用sax解析xml的源代码
java学习笔记——使用DOM解析XML和使用SAX解析XML
sax解析XML方式的代码及特点,sax解析XML方式的代码及特点,sax解析XML方式的代码及特点,sax解析XML方式的代码及特点
NULL 博文链接:https://wang-peng1.iteye.com/blog/1133695
绝对可用的SAX解析XML文件项目,费了九牛二虎之力,才找到的好资源,特与大家共享出来。直接导入eclipse就可运行,无需任何配置。
Android SAX 解析XML SAX 解析XML(通用)
DOM和SAX解析XML文档
JAVA SAX解析XMLJAVA SAX解析XMLJAVA SAX解析XML
sax解析xml本地读取xml,希望对你有用
SAX解析XML源码:安卓客户端程序,通过HTTP协议从服务器端获取XML文件,然后解析并输出到控制台
使用sax解析xml,有详细的解析及源代码实例
NULL 博文链接:https://mazhihui.iteye.com/blog/1535280
Sax解析xml中的某个(一部分报文节点)循环组的内容