package weatherws;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class WeatherUtil {
private static String SERVICES_HOST = "www.webxml.com.cn";
private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
private static String GET_CITY_CODE = WEATHER_SERVICES_URL
+ "getSupportCityString?theRegionCode=";
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode=";
private static String GET_REGION_BY_IP = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getGeoIPContext";
private static String GET_PROVINCE_CODE = WEATHER_SERVICES_URL
+ "getRegionProvince";
private WeatherUtil(){}
public static InputStream getSoapInputStream(String url)
{
InputStream is = null;
try {
URL U = new URL(url);
URLConnection conn = U.openConnection();
conn.setRequestProperty("Host", SERVICES_HOST);
conn.connect();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
public static List<String> getWeather(int cityCode)
{
List<String> weatherList = new ArrayList<String>();
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(WEATHER_QUERY_URL
+ cityCode);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
is.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return weatherList;
}
public static void main(String[] args) throws Exception
{
getRegion();
String[] pInfo = getRegion();
String provinceName = pInfo[0];
String cityName = pInfo[1];
int provinceCode = getProvinceCode(provinceName);
int cityCode = getCityCode(provinceCode, cityName);
List<String> weatherList = WeatherUtil.getWeather(cityCode);
for (String weather : weatherList)
{
System.out.println(weather);
}
}
public static String[] getRegion()
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
String[] regionInfo = new String[5];
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_REGION_BY_IP);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
if(i == 1)
{
Node n = nl.item(i);
String[] provinceInfo = n.getFirstChild().getNodeValue().split("省");
String province = provinceInfo[0];
String[] cityInfo = provinceInfo[1].split("市");
String city = cityInfo[0];
regionInfo[0] = province;
regionInfo[1] = city;
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return regionInfo;
}
public static int getProvinceCode(String provinceName)
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int provinceCode = 0;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_PROVINCE_CODE);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String pName = address[0];
String pCode = address[1];
if(pName.equalsIgnoreCase(provinceName))
{
provinceCode = Integer.parseInt(pCode);
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return provinceCode;
}
public static int getCityCode(int provinceCode, String cityName)
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int cityCode = 0;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_CITY_CODE + provinceCode);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String cName = address[0];
String cCode = address[1];
if(cName.equalsIgnoreCase(cityName))
{
cityCode = Integer.parseInt(cCode);
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return cityCode;
}
}
分享到:
相关推荐
调用pb开发的webserver(SOAP 1.1) /* POST /webservice/n_webservice.asmx HTTP/1.1 Host: localhost Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soap12:Envelope xmlns:xsi=...
调用pb开发的webserver(SOAP 1.2) /* POST /webservice/n_webservice.asmx HTTP/1.1 Host: localhost Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soap12:Envelope xmlns:xsi=...
java中实现xmlschema验证文件借鉴.pdf
第二步:在主form上放上一个按钮和一个Httprio组件(在WebServices页上),并引用第二个单元(即通过Soap Services Importer自动生成的单元) 在Httprio的属性页上的WsdlLocation里面填上...
JAVA SOAP 用到的所有jar文件 axis.jar axis-ant.jar commons-discovery-0.2.jar jaxrpc.jar axis-schema.jar wsdl4j-1.5.1.jar saaj.jar xmlsec-1.3.0.jar
ava校验XML是件很扯淡的事情,... 下面不扯这些废话了,我用DOM4j结合javaxml api使用XSD来校验一个xml有效性,下面这个例子虽然和蹩脚,但是也费了一番功夫,放出来大家研究吧,有更好的实现也希望能和我一同分享。
数字化变电站标准IEC 61850的变电站描述语言SCL采用XML标准,这是该XML语言的模板(Schema)文件。
JSON Schema 校验库——json-schema-validator(java版本).rar
XSD根据xml生成schema文件
JSON Schema 生成库——json-schema-inferrer(java版).rar
Schema校验java相关实现
WebService入门教程_Schema学习示例代码
Node-SOAP 是基于 Node.js 的 SOAP 客户端和服务器。 该模块可以让你使用SOAP连接到Web服务。它还提供了一个服务器,它允许你运行你自己的SOAP服务。特性非常简单的 API可以处理 RPC 和 Document schema 类型支持...
IEC61850 SCL文件Schema校验 1、支持Schema1.0和Schema2.0版本 2、支持自定义Schema版本 3、支持信息输出,格式包括PDF,CSV,TXT
MyEclipse Web Services是建立在XFire Java SOAP框架和工具基础上的, XFire是一款开源的Java SOAP框架。它拥有一个轻量级的信息处理模块,通过STAX来与SOAP信息相结合。提供了一个工作在Web Services下的简单API,...
Oracle9i数据类型Java数据类型Schema类型对比
java中实现xmlschema验证文件参照.pdf
java根据xml生成schema_xsd_生成javabean
java json schema 2.2.6
schema简单文件编写 课程资源 简单代码