把服务器返回来的XML文件翻译成java类

前端之家收集整理的这篇文章主要介绍了把服务器返回来的XML文件翻译成java类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个是在给国付宝服务器发送过去一个连接(带各种参数 你懂的。。),国付宝会自动返回一个XML树,把返回来的XML转换成Java类 GopayWithdraw

前提是我用了httpClient PostMethod发送给国付宝的方式:

public static PostMethod getPostMethod(GopayWithdraw gopay) {
		PostMethod pMethod = new PostMethod(gopay.getGateWay());

		pMethod.addParameter("version",gopay.getVersion());
		pMethod.addParameter("tranCode",gopay.getTranCode());
		pMethod.addParameter("customerId",gopay.getCustomerId());
		pMethod.addParameter("merOrderNum",gopay.getMerOrderNum());
		pMethod.addParameter("tranAmt",gopay.getTranAmt());
		pMethod.addParameter("recvBankAcctName",gopay.getRecvBankAcctName());
		pMethod.addParameter("recvBankProvince",gopay.getRecvBankProvince());
		pMethod.addParameter("recvBankCity",gopay.getRecvBankCity());
		pMethod.addParameter("recvBankName",gopay.getRecvBankName());
		pMethod.addParameter("recvBankBranchName",gopay.getRecvBankBranchName());
		pMethod.addParameter("recvBankAcctNum",gopay.getRecvBankAcctNum());
		pMethod.addParameter("corpPersonFlag",gopay.getCorpPersonFlag());
		pMethod.addParameter("tranDateTime",gopay.getTranDateTime());
		pMethod.addParameter("description","");
		pMethod.addParameter("merchantEncode",gopay.getMerchantEncode());
		pMethod.addParameter("merURL",gopay.getMerURL());
		pMethod.addParameter("signValue",gopay.getSignValue());
		return pMethod;
	}
<pre name="code" class="java"> GopayWithdraw gopayWith = new GopayWithdraw();

 PostMethod pMethod=getPostMethod(gopayWith )
int statusCode = GopayWithdrawHttpClient.executeMethod(pMethod);
if (statusCode == HttpStatus.SC_OK) {
GopayWithdraw gopayWith =getGopayWithdraw(parser,pMethod);//该方法就是把XML转化成java类
return gopayWith //这个gopayWith 就是把服务器返回来的XML树转化成的Java实体类
}

 


public static GopayWithdraw getGopayWithdraw(SAXParser parser,PostMethod pMethod) {
		GopayWithdraw gopayWith = new GopayWithdraw();
		try {
			parser = SAXParserFactory.newInstance().newSAXParser();
			Digester digester = new Digester(parser);
			digester.setValidating(false);
			digester.addObjectCreate("GopayAPIResp",GopayWithdraw.class);
			digester.addSetProperties("GopayAPIResp");
			// digester.addCallMethod("GopayAPIResp/version","version",// 0);
			// digester.addCallMethod("GopayAPIResp/tranCode",// "tranCode",1);
			digester.addBeanPropertySetter("GopayAPIResp/version","version");
			digester.addBeanPropertySetter("GopayAPIResp/tranCode","tranCode");
			digester.addBeanPropertySetter("GopayAPIResp/customerId","customerId");
			digester.addBeanPropertySetter("GopayAPIResp/merOrderNum","merOrderNum");
			digester.addBeanPropertySetter("GopayAPIResp/merURL","merURL");
			digester.addBeanPropertySetter("GopayAPIResp/tranAmt","tranAmt");
			digester.addBeanPropertySetter("GopayAPIResp/recvBankAcctName","recvBankAcctName");
			digester.addBeanPropertySetter("GopayAPIResp/recvBankProvince","recvBankProvince");
			digester.addBeanPropertySetter("GopayAPIResp/recvBankCity","recvBankCity");
			digester.addBeanPropertySetter("GopayAPIResp/recvBankName","recvBankName");
			digester.addBeanPropertySetter("GopayAPIResp/recvBankBranchName","recvBankBranchName");
			digester.addBeanPropertySetter("GopayAPIResp/recvBankAcctNum","recvBankAcctNum");
			digester.addBeanPropertySetter("GopayAPIResp/corpPersonFlag","corpPersonFlag");
			digester.addBeanPropertySetter("GopayAPIResp/tranDateTime","tranDateTime");
			digester.addBeanPropertySetter("GopayAPIResp/description","description");
			digester.addBeanPropertySetter("GopayAPIResp/respCode","respCode");
			digester.addBeanPropertySetter("GopayAPIResp/msgExt","msgExt");
			digester.addBeanPropertySetter("GopayAPIResp/orderId","orderId");
			digester.addBeanPropertySetter("GopayAPIResp/feeAmt","feeAmt");
			digester.addBeanPropertySetter("GopayAPIResp/totalAmount","totalAmount");
			digester.addBeanPropertySetter("GopayAPIResp/SignValue","signValue");
			digester.addBeanPropertySetter("GopayAPIResp/ErrorCode","errorCode");
			digester.addBeanPropertySetter("GopayAPIResp/ErrorMsg","errorMsg");
			try {
				gopayWith = (GopayWithdraw) digester
						.parse(new ByteArrayInputStream(pMethod
								.getResponseBodyAsString().getBytes()));
			} catch (IOException 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();
		}
		return gopayWith;
	}

猜你在找的XML相关文章