纯java方式访问远程WebService接口返回的xml格式的数据保存在本地

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
 * 概述:纯java方式访问远程WebService接口返回的xml格式的数据保存在本地
 */

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 = "D:/netDataToLocalFile.xml";

	private DomXMLString() {}

	public static void main(String[] args) throws Exception {
		Document document = getProvinceCode(NETDATA_URL);
		helloOK(document, LOCAL_PC_SAVEFILE_URL);
	}

	/* 返回一个Document对象 */
	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;
	}

	/* 返回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相关
			urlConn.connect();
			inputStream = urlConn.getInputStream();
		} 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 = 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());
		}

	}

}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring项目中,可以通过使用Spring Web Services框架来创建Web服务。在返回指定格式数据方面,可以使用Spring的消息转换器(Message Converters)来实现。 以下是一个示例代码,演示如何使用Spring Web Services框架和消息转换器来返回JSON格式数据: ```java import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyWebService { @RequestMapping(value = "/my-webservice", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public MyResponseObject myWebServiceMethod() { MyResponseObject response = new MyResponseObject(); // populate response object return response; } } ``` 在上面的示例代码中,我们使用 @RequestMapping 注解来映射一个Web服务方法。我们指定了 produces 参数来指定响应的内容类型为 JSON。我们还使用 @ResponseBody 注解来指示Spring将返回值转换为JSON格式并作为响应体发送。 需要注意的是,要使用消息转换器,需要在Spring的配置文件中添加以下配置: ```xml <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> </bean> </list> </property> </bean> ``` 在上面的配置中,我们添加了一个用于转换JSON格式的消息转换器。这个转换器将会被RequestMappingHandlerAdapter自动识别,并用于将响应转换为JSON格式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值