例子:
代码第16行打开一个地址的连接,返回HttpURLConnection对象。
第22行将要传送的XML内容写入HttpURLConnection的 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,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(); } } }原文链接:https://www.f2er.com/xml/300002.html