xml解析

1.工具类XmlUtils:xml解析

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

/**
 * @author fanggt xml解析
 */
public class XmlUtils {
	// 文档
	private static Document doc = null;
	// 根节点
	private static Element root = null;

	public static void init(String xml) {
		// SAXReader reader = new SAXReader();
		try {
			doc = DocumentHelper.parseText(xml);
			root = doc.getRootElement();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取名称为nodeName的文本
	 * 
	 * @param nodeName
	 * @return
	 */
	public static String getNodeText(String nodeName) {
		Node n = root.selectSingleNode("//" + nodeName);
		return n.getText();
	}

	/**
	 * 获取所有名称为nodeName的节点
	 * 
	 * @param nodeName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Node> getNodeList(String nodeName) {
		return root.selectNodes("//" + nodeName);
	}

	/**
	 * 获取节点名称为nodeName的attrName属性
	 * 
	 * @param nodeName
	 * @param attrName
	 * @return
	 */
	public static String getNodeAttribute(String nodeName, String attrName) {
		Node node = root.selectSingleNode("//" + nodeName);
		return node.valueOf("@" + attrName);
	}

	/**
	 * 获取节点名称为nodeName的attrName属性
	 * 
	 * @param nodeName
	 * @param attrName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getNodeAttribute(String nodeName, String attrName, int index) {
		List<Node> nodes = root.selectNodes("//" + nodeName + "[@" + attrName + "]");
		return nodes.get(index).valueOf("@" + attrName);
	}

	/**
	 * 获取节点名称为nodeName的attrName属性 条件:改节点里面有个属性为key值为value的属性
	 * 
	 * @param nodeName
	 * @param attrName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getNodeAttribute(String nodeName, String attrName, String key, String value) {
		List<Node> nodes = root.selectNodes("//" + nodeName + "[@" + key + "='" + value + "']");
		return nodes.get(0).valueOf("@" + attrName);
	}

}

2.HttpUtils工具类

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpUtils {
    /**
	 * 发送HTTP请求的API
	 * @param url	网址
	 * @param charset	字符集
	 * @param params	Map[String,Object] 请求参数集合
	 * @return	返回访问网址后的HTTP响应结果文本
	 */
	public static String httpRequest(String url, String charset, String body) throws Exception {
		HttpClient client = new HttpClient();
		client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);

		PostMethod postMethod = null;
		try {

			postMethod = new PostMethod(url);
			RequestEntity requestEntity = new StringRequestEntity(body, "text/xml", "UTF-8");
			postMethod.setRequestEntity(requestEntity);
			int statusCode = client.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				String msg = "访问失败!!HTTP_STATUS=" + statusCode;
				throw new HttpException(msg);
			}// if

			String context = postMethod.getResponseBodyAsString();
			return context;

		} finally {
			if (postMethod != null)
				postMethod.releaseConnection();
		}// finally
	}// method
}




3.发送及接受xml格式的报文

String xml_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
				"<MESSAGE>"+
				"<HEAD>"+
				"<TRX_CODE>TP1001</TRX_CODE>"+
				"<BUSINESS_CODE>"+mpd.getPayOrg()+"</BUSINESS_CODE>"+
				"<REQ_SN>" + reqSn +"</REQ_SN>"+
				"<TIMESTAMP>" + timeStamp + "</TIMESTAMP>"+
				"<SIGNED_MSG>";
		String xml_2 = 	"</SIGNED_MSG>"+
				"</HEAD>"+
				"<BODY>"+
				"<ACCOUNT_TYPE>" + mpd.getAccountType() + "</ACCOUNT_TYPE>"+
				"<BANK_NAME>" + mpd.getBankName() + "</BANK_NAME>"+
				"<ACCOUNT_NO>" + mpd.getAccountNo() + "</ACCOUNT_NO>"+
				"<ACCOUNT_NAME>" + mpd.getAccountName() + "</ACCOUNT_NAME>"+
				"<PROVINCE>" + mpd.getProvince() + "</PROVINCE>"+
				"<CITY>" + mpd.getCity() + "</CITY>"+
				"<CNAPS_NAME>" + mpd.getCnapsName() + "</CNAPS_NAME>"+
				"<CNAPS_NO>" + mpd.getCnapsNo() + "</CNAPS_NO>"+
				"<AMOUNT>" + amount + "</AMOUNT>"+
				"</BODY>"+
				"</MESSAGE>";
		// 进行签名的方法
		String signeddata = MD5.toMD5(xml_1 + xml_2 + MD5_KEY);//根据秘钥计算签名
		String body = xml_1 + signeddata + xml_2;
		System.out.println("--------------发送报文----------------------------");
		String res_xml = HttpUtils.httpRequest("http://192.168.**.**:8080/t/trans", "UTF-8", body);
		System.out.println("--------------接收报文----------------------------");

		Map<String, String> result = new HashMap<String, String>();

		XmlUtils.init(res_xml);
		reqSn = XmlUtils.getNodeText("REQ_SN");
		String respCode = XmlUtils.getNodeText("RESP_CODE");
		String respMsg = XmlUtils.getNodeText("RESP_MSG");
		result.put("respCode", respCode);
		result.put("respMsg", respMsg);
		result.put("reqSn", reqSn);
		return result;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值