一个获得天气情况及国家城市的 Web Service
http://www.webservicex.net/globalweather.asmx?WSDL
AXIS2 下载地址 http://ws.apache.org/axis2/download.cgi
其eclipse工具 http://ws.apache.org/axis2/tools/index.html
Code Generator Wizard - Eclipse Plug-in 可以以eclipse中的link方式安装.即可以通过自动
生成java service code.
具体步骤如下:
在eclipse的java project中 NEW --> Other --> Axis2 Wizards -->Axis2 Code Generator
NEXT --> 选择 Generate java source code from a WSDL file
NEXT --> 在WSDL file location: 中输入 : http://www.webservicex.net/globalweather.asmx?WSDL
NEXT --> NEXT --> 选择好文件生成路径
如: E:\eclipseworkspace\axis213\src
FINISH 后会自动生成两个文件:
GlobalWeatherCallbackHandler.java 和 GlobalWeatherStub.java
新建一个测试文件GlobalWeatherTest.java.
内容如下:
package net.webservicex.www;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.rmi.RemoteException;
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.InputSource;
import org.xml.sax.SAXException;
public class GlobalWeatherTest {
public static void main(String[] args) throws RemoteException {
GlobalWeatherStub stub = new GlobalWeatherStub();
GlobalWeatherStub.GetCitiesByCountry request = new GlobalWeatherStub.GetCitiesByCountry();
request.setCountryName("Korea");
GlobalWeatherStub.GetCitiesByCountryResponse response = stub
.GetCitiesByCountry(request);
System.out.println("=================国家城市=================");
//System.out.println(response.getGetCitiesByCountryResult());
String xml = response.getGetCitiesByCountryResult();
parseXML(xml);
GlobalWeatherStub.GetWeather weatherRequest = new
GlobalWeatherStub.GetWeather();
weatherRequest.setCountryName("Korea");
weatherRequest.setCityName("Seoul");
GlobalWeatherStub.GetWeatherResponse weatherResponse =
stub.GetWeather(weatherRequest);
System.out.println("=================国家/城市/天气=================");
System.out.println(weatherResponse.getGetWeatherResult());
}
public static void parseXML(String xml) {
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
StringReader rd = new StringReader(xml);
InputSource is = new InputSource(rd);
Document doc = dombuilder.parse(is);
Element root = doc.getDocumentElement();
NodeList citys = root.getChildNodes();
if (citys != null) {
for (int i = 0; i < citys.getLength(); i++) {
Node city = citys.item(i);
if (city.getNodeType() == Node.ELEMENT_NODE) {
for (Node node = city.getFirstChild(); node != null; node = node
.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("Country")) {
String country = node.getFirstChild()
.getNodeValue();
System.out.print(country);
}
if (node.getNodeName().equals("City")) {
String cityname = node.getFirstChild()
.getNodeValue();
System.out.println(" || " + cityname);
}
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果如下:
=================国家城市=================
Korea, Republic of || Kwangju Ab
Korea, Republic of || Kunsan Ab
Korea, Republic of || Yosu Airport
Korea, Republic of || Chunchon Ab
Korea, Republic of || Hoengsong Ab
Korea, Republic of || Kangnung Ab
Korea, Republic of || Wonju
Korea, Republic of || Cheju International Airport
Korea, Republic of || Pusan / Kimhae International Airport
Korea, Republic of || Mosulpo Ab
Korea, Republic of || Sach'On Ab
Korea, Republic of || Ulsan
Korea, Republic of || Tonghae Radar Site
Korea, Republic of || Seoul / Yongdungp'O Rokaf Wc
Korea, Republic of || Pyongtaek Ab
Korea, Republic of || Seoul
Korea, Republic of || Seoul E Ab
Korea, Republic of || Koon-Ni Range
Korea, Republic of || Osan Ab
Korea, Republic of || Paengnyongdo Ab
Korea, Republic of || Yeonpyeungdo
Korea, Republic of || Seoul / Kimp'O International Airport
Korea, Republic of || Yeoju Range
Korea, Republic of || Suwon Ab
Korea, Republic of || Camp Stanley / H-207
Korea, Republic of || Yongsan / H-208 Hp
Korea, Republic of || Andong
Korea, Republic of || Paekado
Korea, Republic of || Taejon Kor-Afb
Korea, Republic of || Songmu Ab
Korea, Republic of || Taejon
Korea, Republic of || Pohang Ab
Korea, Republic of || Jung Won Rok-Ab
Korea, Republic of || Mangilsan Ab
Korea, Republic of || Taegu Ab
Korea, Republic of || Sangju
Korea, Republic of || Taegu
Korea, Republic of || Chongju Ab
Korea, Republic of || Woong Cheon
Korea, Republic of || Yechon Ab
Korea, Democratic People's Republic of || Kimchaek
Korea, Democratic People's Republic of || Pyongyang
=================国家/城市/天气=================
WSDL