解析格式为纯字符串的MAP封装起来的XML:(2)

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<env:Envelope xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
</env:Header>
<env:Body env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<m:importTaskInfoXmlResponse xmlns:m="http://eoa2.paic.com.cn">
<result xsi:type="xsd:string">
<com.paic.eoa.biz.service.webservice.dto.ReturnResult>
  <taskId>N0000000000101342875</taskId>
  <bool>true</bool>
</com.paic.eoa.biz.service.webservice.dto.ReturnResult>
</result>
</m:importTaskInfoXmlResponse>
</env:Body>
</env:Envelope>

获取taskID与BOOL值:

package com.paic.incentive.core.common.eoa.integration.sao.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.paic.incentive.core.common.eoa.integration.sao.EoaSAO;
import com.paic.incentive.core.reward.award.personnel.dto.EoaDTO;


@Component("eoaSao")
public class EoaSaoImpl implements EoaSAO {
	
	@Value("${eoaWsdl}")
	private String eoaWsdl;

	public StringBuffer buildSoapXml(Map map) {
		StringBuffer sb = new StringBuffer();
		String requestSysID = "HRMSV3_INCENTIVE"+ System.currentTimeMillis();
		String requestSysName = "HRMSV3_INCENTIVE";
		String taskDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		
		sb.append("<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eoa2=\"http://eoa2.paic.com.cn\">");
		sb.append("<soapenv:Header/>");
		sb.append("<soapenv:Body>");
		sb.append("<eoa2:importTaskInfoXml soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
		sb.append("<taskInfo xsi:type=\"xsd:string\">");
		sb.append("<![CDATA[<com.paic.eoa.biz.service.webservice.dto.TaskInfo>");
		
		//taskInfo
		EoaDTO mpInfo = (EoaDTO) map.get("taskInfo");
		sb.append("<requestSysID>"+ requestSysID +"</requestSysID>"); //业务系统唯一ID
		sb.append("<requestSysName>"+ requestSysName +"</requestSysName>");                 //系统来源标记
		sb.append("<taskName>"+ mpInfo.getTaskName() +"</taskName>");                     //文件标题
		sb.append("<taskSortID>" + mpInfo.getTaskSortID() + "</taskSortID>");             //文件种类
		sb.append("<taskDesc>" + mpInfo.getTaskDesc() + "</taskDesc>");           		//文件描述
		sb.append("<taskDate>"+ taskDate +"</taskDate>");                     				//文件申请时间
		sb.append("<taskOwner>"+ mpInfo.getTaskOwner() +"</taskOwner>");                  //文件申请者帐号
//		sb.append("<faEmpNo>" + mpInfo.getFaEmpNo() +"</faEmpNo>");                       //文件申请者编号
		sb.append("<faName>"+ mpInfo.getFaName() +"</faName>");                           //文件申请者姓名
		sb.append("<taskTransactor>"+ mpInfo.getTaskTransactor() +"</taskTransactor>");   //文件经办人及电话
		sb.append("<isNewFlow>"+ mpInfo.getIsNewFlow() +"</isNewFlow>");
		
		//taskDetail
		List<EoaDTO> taskDetail = (List<EoaDTO>) map.get("taskDetail");
		sb.append("<taskDetail>");
		for (EoaDTO eoa2 : taskDetail) {
			sb.append("<com.paic.eoa.biz.service.webservice.dto.TaskDetail>");
			sb.append("<flowOwner>"+ eoa2.getFlowOwner() +"</flowOwner>");
			sb.append("<seq>"+ eoa2.getSeq() +"</seq>");
			sb.append("<handType>"+eoa2.getHandType()+"</handType>");
			sb.append("<nodeDesc>"+ eoa2.getNodeDesc() +"</nodeDesc>");
			sb.append("</com.paic.eoa.biz.service.webservice.dto.TaskDetail>");
		}
		sb.append("</taskDetail>");
		
		//taskFile
		List<EoaDTO> taskFile = (List<EoaDTO>) map.get("taskFile");
		if(taskFile.size() != 0){
			sb.append("<taskFile>");
			for (EoaDTO eoa3 : taskFile) {
				sb.append("<com.paic.eoa.biz.service.webservice.dto.TaskFile>");
				sb.append("<requestSysID>"+ requestSysID +"</requestSysID>");         //业务系统唯一ID
				sb.append("<requestSysName>"+ requestSysName +"</requestSysName>");   //系统来源标记
				sb.append("<docName>"+ eoa3.getDocName() +"</docName>");   
				sb.append("<docOldName>"+ eoa3.getDocOldName() +"</docOldName>");   
				sb.append("<operator>"+ eoa3.getOperator() +"</operator>");   
				sb.append("<operateTime>"+ taskDate +"</operateTime>");   
				sb.append("<docClass>"+ eoa3.getDocClass() +"</docClass>");   
				sb.append("<contentType>"+ eoa3.getContentType() +"</contentType>");   
				sb.append("</com.paic.eoa.biz.service.webservice.dto.TaskFile>");
			}
			sb.append("</taskFile>");
		}
		
		sb.append("</com.paic.eoa.biz.service.webservice.dto.TaskInfo>]]></taskInfo></eoa2:importTaskInfoXml></soapenv:Body></soapenv:Envelope>");
		return sb;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map callEoa(Map map) {
		
		byte[] reqHeadData;
		try {
			reqHeadData = buildSoapXml(map).toString().getBytes("UTF-8");
			PostMethod postMethod = null;
			InputStream is = null;
			HttpClient httpClient = new HttpClient();
			//配置文件读取
			postMethod = new PostMethod(eoaWsdl);
			postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
					new DefaultHttpMethodRetryHandler());
			is = new ByteArrayInputStream(reqHeadData, 0, reqHeadData.length);
			RequestEntity re = new InputStreamRequestEntity(is,
					reqHeadData.length, "application/soap+xml; charset=UTF-8");
			postMethod.setRequestHeader("SOAPAction", "importTaskInfoXml");
			postMethod.setRequestEntity(re); 
			// 执行postMethod 
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode == HttpStatus.SC_OK) {
//				// 读取内容
				byte[] receiveBytes = postMethod.getResponseBody();
				
				//文件的状态(TASKSTATUS): COMPLIED:文件归档; REJECTED:文件终止(不同意且终止); ABANDONED:文件作废; DRAFTDELETE:草稿箱签报作废。
				String taskstatus = "";
				String taskId = "";
				
				//增加xml返回值的解析,读取出签报号等信息
				ByteArrayInputStream bis = new ByteArrayInputStream(receiveBytes);
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse(bis);
				Element e = doc.getDocumentElement();
				
				//解析XML
				String src = e.getTextContent();
				
				ByteArrayInputStream bis1 = new ByteArrayInputStream(src.getBytes("UTF-8"));
				DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance();
				DocumentBuilder db1 = dbf1.newDocumentBuilder();
				Document doc1 = db1.parse(bis1);
				Element e1 = doc1.getDocumentElement();
				
				NodeList list = e1.getElementsByTagName("taskId");
				taskId = list.item(0).getTextContent();
				
				list = e1.getElementsByTagName("bool");
				taskstatus = list.item(0).getTextContent().toString();
				if("true".equals(taskstatus)){
					taskstatus = "1";
				}else{
					taskstatus = null;
					list = e1.getElementsByTagName("exceptionStr");
					String exceptionStr = list.item(0).getTextContent().toString();
					System.out.println("EOA_exception:"+exceptionStr);
					return null;
				}
//				Element element = null;
//				Node taskId = null;
//				/*
//				文件的状态(TASKSTATUS):
//				COMPLIED:文件归档;
//				REJECTED:文件终止(不同意且终止);
//				ABANDONED:文件作废;
//				DRAFTDELETE:草稿箱签报作废。
//				*/
//				Node taskstatus = null;
//				for(int i=0;i<nodeList.getLength();i++){
//					element = (Element) nodeList.item(i); 
//					Node node = (Node) element.getElementsByTagName("string").item(0);
//					if(node.getTextContent().equals("TASKID")){
//						node = (Node) element.getElementsByTagName("string").item(1);
//						taskId = node;
//						continue;
//					}else if(node.getTextContent().equals("TASKSTATUS")){
//						node = (Node) element.getElementsByTagName("string").item(1);
//						taskstatus = node;
//						continue;
//					}
//				}
//				SAXReader saxReader = new SAXReader();
//				saxReader.setEncoding("UTF-8");  
//				Document doc;
//				try {
//					doc = saxReader.read(new String(receiveBytes));
//					Element element =  doc.getRootElement();//获取根元素
//					Map mp = getInfo(element);
//					map.put("taskId", mp.get("taskId"));
//					map.put("taskstatus", mp.get("taskstatus"));
//				} catch (DocumentException e) {
//					e.printStackTrace();
//					return null;
//				}
//			} else {
//				map.put("taskId", null);
//				map.put("status", null);
//			}
				map.put("taskId", taskId);
				map.put("status", taskstatus);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (SAXException e1) {
			e1.printStackTrace();
		}
		return map;
	}
	
//	//递归遍历方法 
//	public static Map getInfo(Element root){
//		Map map = new HashMap();
//		String taskId = "";
//		String taskstatus = "";
//	    @SuppressWarnings("unchecked")
//		Iterator<Element> iter = root.elementIterator();
//	    while (iter.hasNext()) {
//			Element property = (Element) iter.next(); // 获取第三级节点
//			if (("taskId").equals(property.getName().toString())) { // 判断第三级节点的名称是否为name,是:获取文本值显示、否:不显示
//				taskId = property.getText();
//				map.put("taskId", taskId);
//			}else if("bool".equals(property.getName().toString())){
//				taskstatus = property.getText();
//				map.put("taskstatus", taskstatus);
//			}
//			getInfo(property);//自己调用自己 递归方法
//		}
//	    return map;
//	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值