Java获取新浪天气预报代码

来源:互联网 发布:js控制滚动条更细腻 编辑:程序博客网 时间:2024/05/18 02:48

原文地址:http://blog.csdn.net/huiwenjie168/article/details/27332779


  1. package
     com.test.commons;  
  2.   
  3. /** 
  4.  * java获取新浪天气预报代码 
  5.  */  
  6. import java.io.FileNotFoundException;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.FileInputStream;  
  10. import java.io.UnsupportedEncodingException;  
  11. import java.net.MalformedURLException;  
  12. import java.net.URL;  
  13. import java.net.URLEncoder;  
  14. import java.util.HashMap;  
  15. import java.util.Map;  
  16.   
  17. import javax.xml.parsers.DocumentBuilder;  
  18. import javax.xml.parsers.DocumentBuilderFactory;  
  19. import javax.xml.parsers.ParserConfigurationException;  
  20.   
  21. import org.w3c.dom.Document;  
  22. import org.w3c.dom.Element;  
  23. import org.w3c.dom.Node;  
  24. import org.w3c.dom.NodeList;  
  25. import org.xml.sax.SAXException;  
  26.   
  27. /** 
  28.  * 解析xml文档,包括本地文档和url 
  29.  * 
  30.  */  
  31. public class CommonsWeatherUtils {  
  32.       
  33.     InputStream inStream;  
  34.       
  35.     Element root;  
  36.   
  37.     public InputStream getInStream() {  
  38.           
  39.         return inStream;  
  40.           
  41.     }  
  42.   
  43.     public void setInStream(InputStream inStream) {  
  44.           
  45.         this.inStream = inStream;  
  46.           
  47.     }  
  48.   
  49.     public Element getRoot() {  
  50.           
  51.         return root;  
  52.           
  53.     }  
  54.   
  55.     public void setRoot(Element root) {  
  56.           
  57.         this.root = root;  
  58.           
  59.     }  
  60.   
  61.     public CommonsWeatherUtils() {  
  62.           
  63.     }  
  64.   
  65.     /** 
  66.      * 通过输入流来获取新浪接口信息 
  67.      * @param inStream 
  68.      */  
  69.     public CommonsWeatherUtils(InputStream inStream) {  
  70.           
  71.         if (inStream != null) {  
  72.               
  73.             this.inStream = inStream;  
  74.               
  75.             DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();  
  76.               
  77.             try {  
  78.                   
  79.                 DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
  80.                   
  81.                 Document doc = domBuilder.parse(inStream);  
  82.                   
  83.                 root = doc.getDocumentElement();  
  84.                   
  85.             } catch (ParserConfigurationException e) {  
  86.                   
  87.                 e.printStackTrace();  
  88.                   
  89.             } catch (SAXException e) {  
  90.                   
  91.                 e.printStackTrace();  
  92.                   
  93.             } catch (IOException e) {  
  94.                   
  95.                 e.printStackTrace();  
  96.                   
  97.             }  
  98.         }  
  99.     }  
  100.   
  101.     public CommonsWeatherUtils(String path) {  
  102.           
  103.         InputStream inStream = null;  
  104.           
  105.         try {  
  106.               
  107.             inStream = new FileInputStream(path);  
  108.               
  109.         } catch (FileNotFoundException e1) {  
  110.               
  111.             e1.printStackTrace();  
  112.               
  113.         }  
  114.           
  115.         if (inStream != null) {  
  116.               
  117.             this.inStream = inStream;  
  118.               
  119.             DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();  
  120.               
  121.             try {  
  122.                   
  123.                 DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
  124.                   
  125.                 Document doc = domBuilder.parse(inStream);  
  126.                   
  127.                 root = doc.getDocumentElement();  
  128.                   
  129.             } catch (ParserConfigurationException e) {  
  130.                   
  131.                 e.printStackTrace();  
  132.                   
  133.             } catch (SAXException e) {  
  134.                   
  135.                 e.printStackTrace();  
  136.                   
  137.             } catch (IOException e) {  
  138.                   
  139.                 e.printStackTrace();  
  140.                   
  141.             }  
  142.         }  
  143.     }  
  144.   
  145.     public CommonsWeatherUtils(URL url) {  
  146.           
  147.         InputStream inStream = null;  
  148.           
  149.         try {  
  150.               
  151.             inStream = url.openStream();  
  152.               
  153.         } catch (IOException e1) {  
  154.               
  155.             e1.printStackTrace();  
  156.               
  157.         }  
  158.           
  159.         if (inStream != null) {  
  160.               
  161.             this.inStream = inStream;  
  162.               
  163.             DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();  
  164.               
  165.             try {  
  166.                   
  167.                 DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
  168.                   
  169.                 Document doc = domBuilder.parse(inStream);  
  170.                   
  171.                 root = doc.getDocumentElement();  
  172.                   
  173.             } catch (ParserConfigurationException e) {  
  174.                   
  175.                 e.printStackTrace();  
  176.                   
  177.             } catch (SAXException e) {  
  178.                   
  179.                 e.printStackTrace();  
  180.                   
  181.             } catch (IOException e) {  
  182.                   
  183.                 e.printStackTrace();  
  184.                   
  185.             }  
  186.         }  
  187.     }  
  188.   
  189.     /** 
  190.      *  
  191.      * @param nodes 
  192.      * @return 单个节点多个值以分号分隔 
  193.      */  
  194.     public Map<String, String> getValue(String[] nodes) {  
  195.           
  196.         if (inStream == null || root==null) {  
  197.               
  198.             return null;  
  199.               
  200.         }  
  201.           
  202.         Map<String, String> map = new HashMap<String, String>();  
  203.           
  204.         // 初始化每个节点的值为null  
  205.         for (int i = 0; i < nodes.length; i++) {  
  206.               
  207.             map.put(nodes[i], null);  
  208.               
  209.         }  
  210.   
  211.         // 遍历第一节点  
  212.         NodeList topNodes = root.getChildNodes();  
  213.           
  214.         if (topNodes != null) {  
  215.               
  216.             for (int i = 0; i < topNodes.getLength(); i++) {  
  217.                   
  218.                 Node book = topNodes.item(i);  
  219.                   
  220.                 if (book.getNodeType() == Node.ELEMENT_NODE) {  
  221.                       
  222.                     for (int j = 0; j < nodes.length; j++) {  
  223.                           
  224.                         for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) {  
  225.                               
  226.                             if (node.getNodeType() == Node.ELEMENT_NODE) {  
  227.                                   
  228.                                 if (node.getNodeName().equals(nodes[j])) {  
  229.                                       
  230.                                     String val = node.getTextContent();  
  231.                                       
  232.                                     String temp = map.get(nodes[j]);  
  233.                                       
  234.                                     if (temp != null && !temp.equals("")) {  
  235.                                           
  236.                                         temp = temp + ";" + val;  
  237.                                           
  238.                                     } else {  
  239.                                           
  240.                                         temp = val;  
  241.                                           
  242.                                     }  
  243.                                       
  244.                                     map.put(nodes[j], temp);  
  245.                                       
  246.                                 }  
  247.                             }  
  248.                         }  
  249.                     }  
  250.                 }  
  251.             }  
  252.         }  
  253.         return map;  
  254.     }  
  255.     public static void main(String[] args) {  
  256.           
  257.         String link="http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";  
  258.           
  259.         try {  
  260.             System.out.println(URLEncoder.encode("重庆""GBK"));  
  261.         } catch (UnsupportedEncodingException e1) {  
  262.             // TODO Auto-generated catch block  
  263.             e1.printStackTrace();  
  264.         }  
  265.           
  266.         URL url;  
  267.           
  268.         try {  
  269.               
  270.             url = new URL(link);  
  271.             CommonsWeatherUtils parser = new CommonsWeatherUtils(url);  
  272.             String[] nodes = {"city","status1","temperature1","status2","temperature2"};  
  273.             Map<String, String> map = parser.getValue(nodes);  
  274.             System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ ");  
  275.         } catch (MalformedURLException e) {  
  276.             e.printStackTrace();  
  277.         }  
  278.   
  279.     }  
  280. }  

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吞下一个李子核怎么办? 宝宝吞了荔枝核怎么办 美吉姆培训不过怎么办 1岁宝宝挑食怎么办 宝宝不愿意开口说话怎么办 自闭症孩子不爱学习怎么办 宝宝不独立走路怎么办 六个月宝宝不认人怎么办 小孩隔奶奶涨怎么办 小孩段奶奶涨怎么办 1岁半还不会说话怎么办 孩子嗓子哑了怎么办 小朋友嗓子哑了怎么办 4周岁宝宝拉肚子怎么办 小孩不肯拉小便怎么办 做销售不爱说话怎么办 我伤害了朋友怎么办 三岁发音不准怎么办 心里憋不住话怎么办 自己不长记性怎么办 孩子不愿意开口说话怎么办 孩子不爱开口说话怎么办 宝宝犟脾气不好怎么办 小孩说话不算话怎么办 孩子说话不算话怎么办 孩子故意不好好说话怎么办 小孩说话吐字不清楚怎么办 腿老是抽筋是怎么办 半夜睡觉脚抽筋怎么办 我不爱说话内向怎么办 小孩子吐字不清怎么办 宝宝前边头发少怎么办 宝宝咬嘴唇龅牙怎么办 小孩老是咬下唇怎么办 五月小孩掉下床怎么办 小孩说话夹舌头怎么办 小孩自闭不说话怎么办 孩子突然不说话怎么办 孩子说话语速慢怎么办 做磁共振不睡觉怎么办 宝宝吃饭不多怎么办