Tools
package com.ambow.invoic.utils;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.HashMap;import java.util.Map; import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c .dom.Element;import org.w3c.dom.Node;import org .w3c.dom.NodeList;import org.xml.sax.SAXException;public class WeatherUtil { InputStream inStream; Element root; public InputStream getInStream() {return inStream;} public void setInStream(InputStream inStream) {this.inStream = inStream;} public Element getRoot () {return root;} public void setRoot(Element root) {this.root = root;} public WeatherUtil span>() {} /** * Obtain Sina interface information through input stream* @param inStream */ span> public WeatherUtil(InputStream inStream) {if span> (inStream != null) {this.inStream = inStream; DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance( ); try {DocumentBuilder domBuilder = domfac.newDocumentBuilder(); Document doc = domBuilder.parse(inStream); root = doc.getDocumentElement();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}} public WeatherUtil(String path) {InputStream inStream = null; try {inStream = new FileInputStream(path);} catch (FileNotFoundException e1) {e1.printStackTrace();} if ( inStream != null) {this.inStream = inStream; DocumentBuilderFactory domfac = DocumentBuilderFactory.newIns tance(); try {DocumentBuilder domBuilder = domfac.newDocumentBuilder(); Document doc = domBuilder.parse(inStream); root = doc.getDocumentElement();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace() ;} catch (IOException e) {e.printStackTrace();}}} public WeatherUtil(URL url) {InputStream inStream = null; try {inStream = url.openStream();} catch (IOException e1) {e1. printStackTrace();} if (inStream != null) {this.inStream = inStream; DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); try {DocumentBuilder domBuilder = domfac.newDocumentBuilder(); Document doc = domBuilder. parse(inStream); root = doc.getDocumentElement();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}} /** * * @pa ram nodes * @return Multiple values of a single node are separated by semicolons*/ public< /span> Map getValue(String[] nodes) {if (inStream == null || root==null) {return span> null;} Map map = new HashMap() ; // Initialize the value of each node to null for (int i = 0; i null );} // Traverse the first node NodeList topNodes = root.getChildNodes(); if (topNodes != null) {for (int i = 0; i if (book.getNodeType() == Node.ELEMENT_NODE) {for (int j = 0; j for span> (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE) {if (node.getNodeName().equals(nodes[j])) {String val = node.getTextContent(); String temp = map.get(nodes[j]); if (temp != null && !temp.equals("")) {temp = temp + ";" + val;} else {temp = val;} map.put(nodes[j], temp);}}}}}}} return map;} /* public static void main(String[] args) throws UnsupportedEncodingException {System.out.println("Please enter what you need to query Location:"); Scanne r input = new Scanner(System.in); String city = input.next(); String city_url = URLEncoder.encode(city, "GBK"); String link="http://php.weather.sina.com. cn/xml.php?city="+city_url+"&password=DJOYnieT8234jlsK&day=0"; URL url; try {url = new URL(link); WeatherUtil parser = new WeatherUtil(url); String[] nodes = {"city" ,"status1","temperature1","status2","temperature2"}; Map map = parser.getValue(nodes); System.out.println(map.get(nodes[0])+" Today during the day: "+map.get(nodes[1])+" Highest temperature: "+map.get(nodes[2])+"℃ Today and night: "+map.get(nodes[3])+" Lowest Temperature: "+map.get(nodes[4])+"℃ ");} catch (MalformedURLException e) {e.printStackTrace();}} */ }
the ajax method in the controller//weather forecast (AJAX) @RequestMapping("weather") < span class="hljs-keyword">public void weather(HttpSession session,HttpServletResponse response ) throws Exception{ String city_url = URLEncoder.encode("Kunshan", "GBK"); //Define the city and encoding format that need to obtain weather information String link="http://php.weather.sina.com.cn/xml. php?city="+city_url+"&password=DJOYnieT8234jlsK&day=0"; //weather station interface address span> URL url; //Declare address object try {url = new URL(link); //Generate url WeatherUtil parser = new WeatherUtil( url); //Hand over the url to the tool class to process and return weather information String[] nodes = {"city" span>,"status1","temperature1"," status2","tempera ture2"}; //Define a string array that accepts weather information Map map = parser.getValue(nodes); //Weather information is converted to Map objects String weather=map.get(nodes[0])+" Today during the day:"+map.get(nodes [1])+" Maximum temperature: "+map.get(nodes[2])+"℃ tonight and night:"+map. get(nodes[3])+" minimum temperature: "+map.get(nodes[4])+"℃"; //Convenient data generation information string //System.out. println(weather); response.setCharacterEncoding("utf-8"); response.getWriter().println(weather); //use AJAX Return weather information session.setAttribute("weather", weather); //Prevent loss of weather information Put it in the session at the same time} catch (MalformedURLException e) {e.printStackTrace();} }
jsp
$(function(){ $.ajax({ url:'weather.htm', type:"post" span>, async:false, dataType:"text", success:function(data){ document.getElementById("weather").innerHTML=data;} }); }
You can Write it yourself in the page and visit the page directly