一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java訪問WebService返回XML數(shù)據(jù)的方法

Java訪問WebService返回XML數(shù)據(jù)的方法

2019-12-20 14:49liuzx32 JAVA教程

這篇文章主要介紹了Java訪問WebService返回XML數(shù)據(jù)的方法,涉及java操作WebService的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了Java訪問WebService返回XML數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/***
* @author xuechong
* 6/11/2010 16:58
* DomXMLString.java
* 概述:純java方式訪問遠(yuǎn)程WebService接口返回的xml格式的數(shù)據(jù)保存在本地
*/
public class DomXMLString{
private static String SERVICES_HOST = "www.webxml.com.cn";
//遠(yuǎn)程WebService接口url
private static String NETDATA_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";
//訪問遠(yuǎn)程WebService接口返回的xml格式的數(shù)據(jù)保存在本地的絕對路徑
private static String LOCAL_PC_SAVEFILE_URL = "E:dataTest/netDataToLocalFile.xml";
private DomXMLString(){}
public static void main(String[] args) throws Exception{
Document document = getProvinceCode(NETDATA_URL);
helloOK(document, LOCAL_PC_SAVEFILE_URL);
}
/*返回一個Document對象*/
public static Document getProvinceCode(String netXMLDataURL){
Document document = null;
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
documentBF.setNamespaceAware(true);
try{
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(netXMLDataURL);
//具體webService相關(guān)
document = documentB.parse(inputStream);
inputStream.close();
}catch(DOMException e){
e.printStackTrace();
return null;
}catch(ParserConfigurationException e){
e.printStackTrace();
return null;
}catch (SAXException e){
e.printStackTrace();
return null;
}catch(IOException e){
e.printStackTrace();
return null;
}
return document;
}
/*返回InputStream對象*/
public static InputStream getSoapInputStream(String url){
InputStream inputStream = null;
try{
URL urlObj = new URL(url);
URLConnection urlConn = urlObj.openConnection();
urlConn.setRequestProperty("Host", SERVICES_HOST);
//具體webService相關(guān)
urlConn.connect();
inputStream = urlConn.getInputStream();
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return inputStream;
}
/*訪問遠(yuǎn)程(WebService)xml數(shù)據(jù)后返回的xml格式字符串并生成為本地文件*/
public static void helloOK(Document document, String savaFileURL){
TransformerFactory transF = TransformerFactory.newInstance();
try{
Transformer transformer = transF.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "YES");
PrintWriter pw = new PrintWriter(new FileOutputStream(savaFileURL));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
System.out.println("生成xml文件成功!");
}catch(TransformerConfigurationException e){
System.out.println(e.getMessage());
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}catch(TransformerException e){
System.out.println(e.getMessage());
}
}
}

希望本文所述對大家的java程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久亚洲精品专区蓝色区 | 99久久精品久久久久久清纯 | 交换朋友夫妇3中文字幕 | 狠狠躁夜夜躁人人爽天天miya | 四虎影视在线影院在线观看观看 | 欧美精品久久久久久久影视 | 狠狠色综合久久久久尤物 | 精品夜夜澡人妻无码AV蜜桃 | 午夜国产精品影院在线观看 | 美国美女hd18 | 国产欧美成人免费观看 | 久久re热在线视频精6 | 啊哈用力cao我 | 欧美在线视频一区 | 91高清国产视频 | 国产精品日本一区二区不卡视频 | 日韩二区三区 | 免费日本视频 | 97精品国产高清在线看入口 | 动漫女性扒开尿口羞羞漫画 | 精品视频免费在线 | 26uuu成人人网图片 | 亚洲精彩视频在线观看 | 国产精自产拍久久久久久 | 亚洲小视频在线 | 成人嗯啊视频在线观看 | 国产91无毒不卡在线观看 | 青柠影院在线观看免费完整版1 | 欧美xxoo做爰猛烈视频 | 麻豆视频入口 | 热99re久久精品精品免费 | 亚洲视频精选 | 精品一区二区三区自拍图片区 | 为什么丈夫插我我却喜欢被打着插 | 69pao强力打造免费高速 | 麻豆天美精东果冻传媒在线 | 亚洲精品福利一区二区在线观看 | 特大黑人娇小亚洲女mp4 | 99精品视频在线观看免费 | 免费观看一级特黄三大片视频 | 5555kkkk香蕉在线观看 |