流接收第三方传递的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();
 }
 
 
 
 
 
 
 
 
 
 
 
 
}









你可能感兴趣的:(流接收第三方传递的xml并解析xml根据键铭获得键值)