码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-01-06 21:57:16      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/a2sha5/p/5107382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!