Java訪問WebService返回XML資料的方法_java

來源:互聯網
上載者:User

本文執行個體講述了Java訪問WebService返回XML資料的方法。分享給大家供大家參考。具體如下:

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方式訪問遠程WebService介面返回的xml格式的資料儲存在本地*/public class DomXMLString{private static String SERVICES_HOST = "www.webxml.com.cn";//遠程WebService介面urlprivate static String NETDATA_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";//訪問遠程WebService介面返回的xml格式的資料儲存在本地的絕對路徑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相關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相關urlConn.connect();inputStream = urlConn.getInputStream();}catch(MalformedURLException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}return inputStream;}/*訪問遠程(WebService)xml資料後返回的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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.