本文实例讲述了Java访问WebService返回XML数据的方法。分享给大家供大家参考。具体如下:
?
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; 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); } 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; } 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; } 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程序设计有所帮助。