免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5084 | 回复: 0
打印 上一主题 下一主题

inputStream 和 xml读取 生成 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-19 16:45 |只看该作者 |倒序浏览
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("
http://www.webxml.com.cn/WebServices/WeatherWS.asmx
");
      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", "
http://WebXml.com.cn/getWeather
");
      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
              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
      
   
  


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/109541/showart_2153358.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP