Java访问WebService返回XML数据的⽅法本⽂实例讲述了Java访问WebService返回XML数据的⽅法。分享给⼤家供⼤家参考。具体如下:
import java.io.IOException;
import java.io.InputStream;
import java.MalformedURLException;
import java.URL;
import java.URLConnection;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
l.sax.SAXException;
l.parsers.DocumentBuilder;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;
l.transform.OutputKeys;
l.transform.Transformer;
l.transform.TransformerConfigurationException;
l.transform.TransformerException;
l.transform.TransformerFactory;
l.transform.dom.DOMSource;
l.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";
//远程WebService接⼝url
private static String NETDATA_URL = "webservice.webxml/WebServices/WeatherWS.asmx/getRegionProvince"; //访问远程WebService接⼝返回的xml格式的数据保存在本地的绝对路径
private static String LOCAL_PC_SAVEFILE_URL = "E:l";
private DomXMLString(){}
public static void main(String[] args) throws Exception{
Document document = getProvinceCode(NETDATA_URL);
webservice实现helloOK(document, LOCAL_PC_SAVEFILE_URL);
}
/*返回⼀个Document对象*/
public static Document getProvinceCode(String netXMLDataURL){
Document document = null;
DocumentBuilderFactory documentBF = wInstance();
documentBF.setNamespaceAware(true);
try{
DocumentBuilder documentB = wDocumentBuilder();
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相关
inputStream = InputStream();
}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 = wInstance();
try{
Transformer transformer = wTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(Out
putKeys.INDENT, "YES"); PrintWriter pw = new PrintWriter(new FileOutputStream(savaFileURL)); StreamResult result = new StreamResult(pw);
System.out.println("⽣成xml⽂件成功!");
}catch(TransformerConfigurationException e){
System.out.Message());
}catch(IllegalArgumentException e){
System.out.Message());
}catch(FileNotFoundException e){
System.out.Message());
}catch(TransformerException e){
System.out.Message());
}
}
}
希望本⽂所述对⼤家的java程序设计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论