The example in this article describes the way Java accesses WebService return XML data. Share to everyone for your reference. as follows:
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.