Java访问WebService返回XML数据的方法
时间:2020-11-26 22:03:19|栏目:JAVA代码|点击: 次
本文实例讲述了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接口url private 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程序设计有所帮助。
上一篇:Spring Boot Admin Server管理客户端过程详解
栏 目:JAVA代码
本文标题:Java访问WebService返回XML数据的方法
本文地址:http://www.codeinn.net/misctech/26419.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机