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: access the xml format data returned by the remote WebService Interface in java mode is saved 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 "; // Access the absolute path of the xml format data returned by the remote WebService Interface stored locally 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 ); } /* Return 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 ); // Related to the specific 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; } /* Return the InputStream object */ Public static InputStream getSoapInputStream (String url ){ InputStream inputStream = null; Try { URL urlObj = new URL (url ); URLConnection urlConn = urlObj. openConnection (); UrlConn. setRequestProperty ("Host", SERVICES_HOST ); // Related to the specific webService UrlConn. connect (); InputStream = urlConn. getInputStream (); } Catch (MalformedURLException e ){ E. printStackTrace (); } Catch (IOException e ){ E. printStackTrace (); } Return inputStream; } /* Access the xml format string returned by the remote (WebService) xml data and generate 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 ("xml file generated successfully! "); } 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 ()); } } } |