The example in this article describes the Java Access WebService method of returning XML data. Share to everyone for your reference. as follows:
?
| 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 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 5 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; /*** * @author Xuechong * 6/11/2010 16:58 * Domxmlstring.java * Overview: Pure Java access to remote WebService interface returns XML-formatted data stored locally/public class domxmlstring{private static String Services_host = "Www.webxmL.com.cn "; Remote WebService interface URL private static String Netdata_url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/ Getregionprovince "; Accessing the XML format data returned by the remote WebService interface is saved in the local absolute path private static String Local_pc_savefile_url = "e:datatest/ Netdatatolocalfile.xml "; Private domxmlstring () {} public static void Main (string[] args) throws exception{Document document = Getprovincecode (NETD Ata_url); Hellook (document, Local_pc_savefile_url); /* Returns a Document Object/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); Specific WebService Related 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; /* * Return InputStream object/public static InputStream getsoapinputstream (String url) {InputStream inputstream = null; try{URL U rlobj = new URL (URL); URLConnection urlconn = Urlobj.openconnection (); Urlconn.setrequestproperty ("Host", services_host); Specific WebService related urlconn.connect (); InputStream = Urlconn.getinputstream (); }catch (malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return inputstream; }/* The XML format string returned after accessing remote (WEBSERVICE) XML data is born into a local file/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 ("Generate XML File Success!"); }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 ());}} } |
I hope this article will help you with your Java programming.