dom方式解析4A服务返回的xml
2016-01-06 20:51
417 查看
4A服务客户端向6+1系统发送数据,分为两次发送,第一次发送获取token,加密后再发送一次,保证数据安全性。
java解析xml一般有4种方法:DOM、SAX、JDOM、DOM4j
这里使用的是DOM
View Code
java解析xml一般有4种方法:DOM、SAX、JDOM、DOM4j
这里使用的是DOM
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.sql.Timestamp; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import cn.csg.soa.pdjyh.client.ServicesKernel; public class GetConnectionByServerRequest { private static Log log = LogFactory .getLog(GetConnectionByServerRequest.class); public String GetTicket(String verificationCode,String type) { log.debug("*****"+type+"******4A***ticket************"); String unitName="PDJYH",message="",returnVal="FAIL"; String extraData=new Timestamp(System.currentTimeMillis()).toString().replace(" ", "T") + "Z"; String returnCode = ""; try { StringBuffer soapString = new StringBuffer(); soapString.append(""); soapString.append(" <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"); soapString.append(" <SOAP-ENV:Body>"); soapString.append(" <m:getConnectionByServerRequest xmlns:m=\"http://soa.csg.cn\">"); soapString.append("<m:unitName>"+unitName+"</m:unitName>"); if(!"".equals(verificationCode)&&"SECOND".equals(type)){ soapString.append("<m:verificationCode>"+verificationCode+"</m:verificationCode>"); } soapString.append("<m:extraData>"+extraData+"</m:extraData>"); soapString.append(" </m:getConnectionByServerRequest>"); soapString.append(" </SOAP-ENV:Body>"); soapString.append(" </SOAP-ENV:Envelope>"); String endpoint = new ServicesKernel().getEndpoint("4A"); PostMethod post = null; HttpClient httpclient = new HttpClient(); post = new PostMethod(endpoint); post.addRequestHeader("SOAPAction", "urn:getConnectionByServerRequest"); RequestEntity entity; entity = new StringRequestEntity(soapString.toString(), "text/xml","UTF-8"); post.setRequestEntity(entity); int result; result = httpclient.executeMethod(post); InputStream res; if (result != 200) { res = post.getResponseBodyAsStream(); throw new Exception("Web Service Server returns HTTP code " + result + "\n" + res); } returnVal=post.getResponseBodyAsString(); log.debug("\n>>>>>>>>>>>>>returnVal>>>>>>>>>>>>>>>\n"+ returnVal); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); NodeList n2 = null; InputSource is =new InputSource(new ByteArrayInputStream(returnVal.getBytes("UTF-8"))); Document doc = builder.parse(is); if(returnVal.indexOf("成功")>0||returnVal.indexOf("1000")>0){ returnVal = doc.getElementsByTagName("soa:message").item(0).getFirstChild().getNodeValue(); System.err.println("**********"+returnVal); }else System.out.println("*****4A-->FAILED********"+returnVal); if("".equals(verificationCode)&&"FIRST".equals(type)){ n2 = doc.getElementsByTagName("soa:verificationCode"); }else if(!"".equals(verificationCode)&&"SECOND".equals(type)){ n2 = doc.getElementsByTagName("soa:token"); } if(null!=n2&&null!=n2.item(0)){ returnCode = n2.item(0).getFirstChild().getNodeValue(); System.err.println("****returnCode******** "+returnCode); } }catch (Exception e) { returnCode=""; log.error("********4A认证服务异常,异常信息:" + e.getMessage()); e.printStackTrace(); }finally{ new ServicesKernel().putInTable("4A",returnVal, message); } return returnCode; } }
View Code
相关文章推荐
- 常用linux命令
- Android使用系统自带的audioreorder和mediaplayer进行录音和播放,外加进度条的滚动!
- Python timer定时器
- maven web project打包为war包,目录结构的变化
- 利用ViewPager实现仿微信Tab效果
- (java)Single Number III
- <LeetCode OJ> 152. Maximum Product Subarray
- 类的三大特性之一:多态[入门程序示例]
- ubuntu安装 LNMP+redis
- 软件工程导论考试-试卷7
- C语言基础总结-数据类型(常用)
- JavaScript unescape() 函数详解
- android静态注册的广播在某些手机上无法正常接收
- 如何画程序流程图
- html学习笔记二
- hadoop安装报错记录
- 初识EL表达式
- 可取消选择的单选框
- Android SDK Manager更新镜像源
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3