您的位置:首页 > 其它

dom方式解析4A服务返回的xml

2016-01-06 20:51 417 查看
4A服务客户端向6+1系统发送数据,分为两次发送,第一次发送获取token,加密后再发送一次,保证数据安全性。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: