Java访问WebService返回XML数据的方法
程序员文章站
2024-03-03 21:44:40
本文实例讲述了java访问webservice返回xml数据的方法。分享给大家供大家参考。具体如下:
import java.io.ioexception;
i...
本文实例讲述了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程序设计有所帮助。
推荐阅读