Java訪問WebService返回XML數(shù)據(jù)的方法
更新時(shí)間:2015年06月08日 17:01:02 作者:liuzx32
這篇文章主要介紹了Java訪問WebService返回XML數(shù)據(jù)的方法,涉及java操作WebService的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了Java訪問WebService返回XML數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:
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ù)保存在本地的絕對(duì)路徑 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); } /*返回一個(gè)Document對(duì)象*/ 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對(duì)象*/ 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()); } } }
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- Java web xml文件讀取解析方式
- Java解析調(diào)用webservice服務(wù)的返回XML串詳解
- web.xml中servlet, bean, filter, listenr 加載順序_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- web.xml詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- JavaWeb中web.xml初始化加載順序詳解
- 淺談JavaWeb中的web.xml配置部署描述符文件
- Javaweb中使用Jdom解析xml的方法
- java web項(xiàng)目里ehcache.xml介紹
- JAVA Web.xml加載順序過程詳解
相關(guān)文章
SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁功能
在SpringBoot項(xiàng)目中,結(jié)合MyBatis-Plus(簡(jiǎn)稱MP)可以非常方便地實(shí)現(xiàn)分頁功能,MP為開發(fā)者提供了分頁插件PaginationInterceptor,只需簡(jiǎn)單配置即可使用,本文給大家介紹了SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁功能,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01SpringBoot 使用Mongo的GridFs實(shí)現(xiàn)分布式文件存儲(chǔ)操作
這篇文章主要介紹了Spring Boot 使用Mongo的GridFs實(shí)現(xiàn)分布式文件存儲(chǔ)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java實(shí)現(xiàn)PDF導(dǎo)出功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)PDF導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-09-09解決SpringBoot框架因post數(shù)據(jù)量過大沒反應(yīng)問題(踩坑)
這篇文章主要介紹了解決SpringBoot框架因post數(shù)據(jù)量過大沒反應(yīng)問題(踩坑),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09妙解Java中的回調(diào)機(jī)制(CallBack)
本文以最簡(jiǎn)明扼要的例子將Java的回調(diào)機(jī)制介紹給大家,感興趣的朋友可以參考一下。2016-07-07