流接收第三方传递的xml并解析xml根据键铭获得键值



<action name="ss" method="testAA" class="com.actions.XMLClient">
 
    </action>
    
<action name="qq" method="cWeChat" class="com.actions.JieShouXml">
   
    </action>


public void testAA() throws ClientProtocolException, IOException {
     XMLClient client = new XMLClient();
    
    
     String xml="<xml>"+
   "<appid>"+20+"</appid>"+
   "<mch_id>"+30+"</mch_id>"+
   "<nonce_str>"+40+"</nonce_str>"+
   "<out_trade_no>"+40+"</out_trade_no>"+
   "</xml>";
     Integer statusCode = client.sendXMLDataByPost("http://localhost:8080/testhttp/qq", xml);
    System.out.println("222222222222222222222222222222222222222222222222222222222222222222222");
     if(statusCode==200){
      System.out.println("Request Success,Response Success!!!");
     }else{
      System.out.println("Response Code :"+statusCode);
     }


package com.actions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import com.PayTransResponse;
import com.opensymphony.xwork2.ActionContext;

public class JieShouXml {
 ActionContext actx = ActionContext.getContext();
 HttpServletRequest request = (HttpServletRequest) actx.get(ServletActionContext.HTTP_REQUEST);
 public void cWeChat() throws Exception {
     System.out.println("进来了");
  InputStream inputStream;
 
   inputStream = request.getInputStream();
  
  String strMessage = "";
  String strResponse = "";
             BufferedReader reader;
             reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
             while ((strMessage = reader.readLine()) != null) {
                 strResponse += strMessage;
                 System.out.println(strResponse);
             }
             Map map = GetWxOrderno.doXMLParse(strResponse);
             System.out.println("11111111111111111111111111111111111111111111111");
             System.out.println(map.get("out_trade_no"));     
             System.out.println(map.get("out_trade_no")); 
         reader.close();
        
     inputStream.close();
    
 }
 }


package com.actions;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.impl.client.DefaultHttpClient;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;




public class GetWxOrderno
{
 
  public static DefaultHttpClient httpclient;

  static
  {
    httpclient = new DefaultHttpClient();
  
  }
 
  public static Map doXMLParse(String strxml) throws Exception {
  if(null == strxml || "".equals(strxml)) {
   return null;
  }
  
  Map m = new HashMap();
  InputStream in = String2Inputstream(strxml);
  SAXBuilder builder = new SAXBuilder();
  Document doc = builder.build(in);
  Element root = doc.getRootElement();
  List list =  root.getChildren();
  Iterator it = list.iterator();
  while(it.hasNext()) {
   Element e = (Element) it.next();
   String k = e.getName();
   String v = "";
   List children = e.getChildren();
   if(children.isEmpty()) {
    v = e.getTextNormalize();
   } else {
    v = getChildrenText(children);
   }
   
   m.put(k, v);
  }
  
  //关闭流
  in.close();
  
  return m;
 }
 
  public static InputStream String2Inputstream(String str) {
  return new ByteArrayInputStream(str.getBytes());
 }
 
  public static String getChildrenText(List children) {
  StringBuffer sb = new StringBuffer();
  if(!children.isEmpty()) {
   Iterator it = children.iterator();
   while(it.hasNext()) {
    Element e = (Element) it.next();
    String name = e.getName();
    String value = e.getTextNormalize();
    List list = e.getChildren();
    sb.append("<" + name + ">");
    if(!list.isEmpty()) {
     sb.append(getChildrenText(list));
    }
    sb.append(value);
    sb.append("</" + name + ">");
   }
  }
  
  return sb.toString();
 }
 
 
 
 
 
 
 
 
 
 
 
 
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值