XML--两个系统采用servlet发送及解析XML

例子:

代码第16行打开一个地址的连接,返回HttpURLConnection对象。

第22行将要传送的XML内容写入HttpURLConnection的Open Declaration OutputStream流中。(注意 这里需要传入byte字节数组)

然后23行调用.getInputStream()此时才会跳转到服务器端servlet,如果不调用.getInputStream()是不会跳转过去的。

客户端:

package adopen;

import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.haier.util.AesUtil;
import com.haier.util.StreamTool;

public class URLTest {

	public static String testServletUrl(String path, String xml)
			throws Exception {
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");// 提交模式
		conn.setConnectTimeout(10000);// 连接超时 单位毫秒
		conn.setReadTimeout(5000);// 读取超时 单位毫秒
		conn.setDoOutput(true);// 是否输入参数
		// 通过conn.getOutputStream().write 将XML信息写入,在另一端系统,get出来再解析
		conn.getOutputStream().write(xml.getBytes("UTF-8"));
		InputStream inStream = conn.getInputStream();
		inStream.close();
		return xml;
	}

	public static String getCampaginXmlInfo() {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<haier_ad>");
		sb.append("    <campName>Campaign</campName>");
		sb.append("    <agent_ID>Agent001</agent_ID>");
		sb.append("    <client_ID>Client001</client_ID>");
		sb.append("    <sDate>2013-07-10 00:00:00</sDate>");
		sb.append("    <eDate>2013-10-10 00:00:00</eDate>");
		sb.append("    <coverID>cover002</coverID>");
		sb.append("</haier_ad>");
		return sb.toString();
	}

	public static void main(String[] args) {
		try {// testPostServlet
			String result = null;
			result = URLTest.testServletUrl(
					"http://localhost:8080/haierAdOpenApi/domTestServlet",
					getCampaginXmlInfo());
			System.out.println("result:" + result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


客户端发送过来的xml文件采用DOM进行解析。详见xml--通过DOM解析XML

注意:我这里用的servlet3.0,采用注解的来替换web.xml中的配置

@WebServlet(urlPatterns = { "/domTestServlet" }, asyncSupported = true),如果您不是servlet3.0版本,还需要在web.xml中配置servlet的跳转信息。

服务端:

package com.haier.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 使用递归解析给定的任意一个xml文档并且将其内容输出到命令行上
 * 
 * @author zhanglong
 * 
 */
@WebServlet(urlPatterns = { "/domTestServlet" }, asyncSupported = true)
public class DomTestServlet extends BaseServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {// step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			// System.out.println("class name: " + dbf.getClass().getName());

			// step 2:获得具体的dom解析器
			DocumentBuilder db = dbf.newDocumentBuilder();

			// System.out.println("class name: " + db.getClass().getName());

			// step3: 解析一个xml文档,获得Document对象(根结点)
			Document document = db.parse(request.getInputStream());

			NodeList list = document.getElementsByTagName("haier_ad");

			for (int i = 0; i < list.getLength(); i++) {
				Element element = (Element) list.item(i);

				String content = element.getElementsByTagName("campName")
						.item(0).getFirstChild().getNodeValue();

				System.out.println("campName:" + content);

				content = element.getElementsByTagName("agent_ID").item(0)
						.getFirstChild().getNodeValue();

				System.out.println("agent_ID:" + content);

				content = element.getElementsByTagName("client_ID").item(0)
						.getFirstChild().getNodeValue();

				System.out.println("client_ID:" + content);

				content = element.getElementsByTagName("coverID").item(0)
						.getFirstChild().getNodeValue();

				System.out.println("coverID:" + content);
			}
		} catch (DOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值