Java 调用远程webservice接口实例(SOAP方式,DOM分析XML)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java 调用远程webservice接口实例(SOAP方式,DOM分析XML)

Java 调用远程webservice接口实例(SOAP方式,DOM分析XML)

 2010/12/25 0:04:35  haiyang08101  http://haiyang08101.javaeye.com  我要评论(0)
  • 摘要:packagemyweather;importjava.io.*;importjava.net.*;importjavax.xml.parsers.*;importorg.w3c.dom.*;importjavax.swing.JOptionPane;publicclassMyWeather{privatestaticStringgetSoapRequest(Stringcity){try{InputStreamReaderisr=newInputStreamReader
  • 标签:Web Service Webservice Java 方式 实例 分析 接口
package myweather;

import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.swing.JOptionPane;

public class MyWeather {
    private static String getSoapRequest(String city) {
     try {
     InputStreamReader isr = new InputStreamReader(new FileInputStream("weather.xml"));
     BufferedReader reader = new BufferedReader(isr);
     String soap = "";
     String tmp;
     while ((tmp = reader.readLine()) != null) {
      soap += tmp;
     }
     reader.close();
     isr.close();
     return soap.replace("${city}$", 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/WeatherWebService.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/getWeatherbyCityName");

                 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 getWeather(String city)
   {
       try
       {
           Document doc;
           DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
           dbf.setNamespaceAware(true);
           DocumentBuilder db=dbf.newDocumentBuilder();
           InputStream is=getSoapInputStream(city);
           doc=db.parse(is);
           NodeList nl=doc.getElementsByTagName("getWeatherbyCityNameResult");
           Node n=nl.item(0);
           //JOptionPane.showMessageDialog(null,n.getChildNodes().getLength());
           for (int i=0;i<n.getChildNodes().getLength()-1;i++)
           {
             String weather=n.getChildNodes().item(i).getTextContent();
             System.out.print(weather);
           }
           is.close();
           return "null";
       }
       catch(Exception e)
       {
           e.printStackTrace();
           return null;
       }
   }
   public static void main (String [] args )throws Exception
   {
       //System.out.println(MyWeather.getWeather("上海"));
       MyWeather.getWeather("广州");
   }

}


//将以下XML保存为weather.xml

/*XML:<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>${city}$</theCityName>
    </getWeatherbyCityName>
</soap:Body>
</soap:Envelope>*/
发表评论
用户名: 匿名