package myprogram.action;
import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.regex.*;
public class WeatherAction {
//构造soap消息
private static String getSoapRequest(String city) {
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"D:/Program Files/weather.xml"));
BufferedReader reader = new BufferedReader(isr);
String soap = "";
String tmp;
while ( (tmp = reader.readLine()) != null) {
soap += tmp;
}
reader.close();
isr.close();
System.out.println(soap.replaceAll("ccccity", city));
return soap.replaceAll("ccccity", city);
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL("");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction", "");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
//osw.write(new String(soap.getBytes(),"utf-8"));
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getWeather1(String city) {
try {
System.out.println("1");
InputStream instr = WeatherAction.getSoapInputStream(city);//获取inputstream
ggr(instr);//创建xml文件
String str=msg(city);//读取xml文件
return str;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
//MyWeather.getWeather("杭州");
WeatherAction.getWeather1("上虞");
}
/**
* 获取xml信息
* @param city String
* @return String
*/
public static String msg(String city){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("test.txt");
NodeList nl = (NodeList) doc.getElementsByTagName("getWeatherResult");
Node n = nl.item(0);
String test="";
String today="";
String tomorrow="";
String threeday="";
String weathervalue="";
String gif="";
for (int i = 0; i < n.getChildNodes().getLength() - 1; i++) {
String weather = n.getChildNodes().item(i).getFirstChild().getNodeValue();
test =test+weather ;
}
return test;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取inputstream 创建xml文件
* @param is InputStream
*/
public static void ggr(InputStream is) {
try {
System.out.println("----------文件输出了没--------");
BufferedInputStream bis = new BufferedInputStream(is);
File f = new File("test.txt");
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(f));
byte[] buffer = new byte[16 * 1024];
int numread = bis.read(buffer);
int i = 0;
while (numread != -1) {
i++;
bos.write(buffer, 0, numread);
String aa = new String(buffer);
System.out.println(aa + "-----" + i+buffer);
bos.flush();
numread = bis.read(buffer);
}
bis.close();
bos.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
weather.xml
ccccity
阅读(1982) | 评论(0) | 转发(0) |