axis2带list的报文,对象和xml的转换
import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.sinovatech.service.NgCallService.NgCallServiceRequest; import com.sinovatech.service.NgCallService.NgCallServiceResponse; import com.sinovatech.service.s2299Query.BusiInfo; import com.sinovatech.service.s2299Query.S2299QueryRequest; import com.sinovatech.service.s2299Query.S2299QueryResponse; import com.sinovatech.service.sQUserBase.sQUserBaseRequest; import com.sinovatech.service.sQUserBase.sQUserBaseResponse; public class MyParseXML { private static final Logger log = Logger.getLogger(MyParseXML.class); private static StringBuffer xmlHead =null; static{ xmlHead = new StringBuffer(); xmlHead.append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?>").append("\n"); xmlHead.append("<ROOT>").append("\n"); } /** * 信用担保次数查询接口 * @param request * @return */ public static String parseS2299QueryRequest2XML(S2299QueryRequest request){ log.info("parseS2299QueryRequest2XML start "); StringBuffer xmlRequest = new StringBuffer(); xmlRequest.append(xmlHead.toString()); xmlRequest.append("<REQUEST_INFO>").append("\n").append("<OPR_INFO>").append("\n"); if(request.getRegionId()!=null){ xmlRequest.append("<REGION_ID type=\"long\">"+request.getRegionId()+"</REGION_ID>").append("\n"); }else{ xmlRequest.append("<REGION_ID type=\"long\"></REGION_ID>").append("\n"); } if(request.getChannelType()!=null){ xmlRequest.append("<CHANNEL_TYPE type=\"long\">"+request.getChannelType()+"</CHANNEL_TYPE>").append("\n"); }else{ xmlRequest.append("<CHANNEL_TYPE type=\"long\"></CHANNEL_TYPE>").append("\n"); } if(request.getLoginNo()!=null){ xmlRequest.append("<LOGIN_NO type=\"long\">"+request.getLoginNo()+"</LOGIN_NO>").append("\n"); }else{ xmlRequest.append("<LOGIN_NO type=\"long\"></LOGIN_NO>").append("\n"); } if(request.getLoginPwd()!=null){ xmlRequest.append("<LOGIN_PWD type=\"long\">"+request.getLoginPwd()+"</LOGIN_PWD>").append("\n"); }else{ xmlRequest.append("<LOGIN_PWD type=\"long\"></LOGIN_PWD>").append("\n"); } if(request.getIpAddress()!=null){ xmlRequest.append("<IP_ADDRESS type=\"long\">"+request.getIpAddress()+"</IP_ADDRESS>").append("\n"); }else{ xmlRequest.append("<IP_ADDRESS type=\"long\"></IP_ADDRESS>").append("\n"); } if(request.getGroupId()!=null){ xmlRequest.append("<GROUP_ID type=\"long\">"+request.getGroupId()+"</GROUP_ID>").append("\n"); }else{ xmlRequest.append("<GROUP_ID type=\"long\"></GROUP_ID>").append("\n"); } if(request.getContactId()!=null){ xmlRequest.append("<CONTACT_ID type=\"long\">"+request.getContactId()+"</CONTACT_ID>").append("\n"); }else{ xmlRequest.append("<CONTACT_ID type=\"long\"></CONTACT_ID>").append("\n"); } if(request.getOpCode()!=null){ xmlRequest.append("<OP_CODE type=\"string\">"+request.getOpCode()+"</OP_CODE>").append("\n"); }else{ xmlRequest.append("<OP_CODE type=\"string\"></OP_CODE>").append("\n"); } xmlRequest.append("</OPR_INFO>").append("\n").append("<BUSI_INFO_LIST>").append("\n"); System.out.println("request.getBusiInfoList().size() "+request.getBusiInfoList().size()); if(request.getBusiInfoList()!=null&&request.getBusiInfoList().size()>0){ for(int i=0;i<request.getBusiInfoList().size();i++){ BusiInfo busiInfo=request.getBusiInfoList().get(i); xmlRequest.append("<BUSI_INFO>").append("\n"); xmlRequest.append("<ID_NO type=\"long\">"+busiInfo.getIdNo()+"</ID_NO>").append("\n"); xmlRequest.append("</BUSI_INFO>").append("\n"); } }else{ xmlRequest.append("<BUSI_INFO>").append("\n"); xmlRequest.append("<ID_NO type=\"long\"></ID_NO>").append("\n"); xmlRequest.append("</BUSI_INFO>").append("\n"); } xmlRequest.append("<BUSI_INFO_LIST>").append("\n") .append("</REQUEST_INFO>").append("\n").append("</ROOT>").append("\n"); log.info("parseS2299QueryRequest2XML end "); return xmlRequest.toString(); } /** * 信用担保次数查询接口 * @param xml * @return * @throws DocumentException */ public static S2299QueryResponse parseS2299QueryResponse(String xml) throws DocumentException{ Document document =DocumentHelper.parseText(xml); S2299QueryResponse response =new S2299QueryResponse(); Element root = document.getRootElement(); String returnCode = root.elementTextTrim("RETURN_CODE"); String returnMsg = root.elementTextTrim("RETURN_MSG"); String userMsg = root.elementTextTrim("USER_MSG"); String detailMsg = root.elementTextTrim("DETAIL_MSG"); String promptMsg = root.elementTextTrim("PROMPT_MSG"); if(returnCode!=null&&(returnCode.equals("0")||returnCode.equals("0"))){ Element OUT_DATA = root.element("OUT_DATA"); String phoneNo = OUT_DATA.elementTextTrim("PHONE_NO"); String custName = OUT_DATA.elementTextTrim("CUST_NAME"); String runName = OUT_DATA.elementTextTrim("RUN_NAME"); String creditName = OUT_DATA.elementTextTrim("CREDIT_NAME"); String creditCode = OUT_DATA.elementTextTrim("CREDIT_CODE"); String idIccid = OUT_DATA.elementTextTrim("ID_ICCID"); String idAddress = OUT_DATA.elementTextTrim("ID_ADDRESS"); String vipDelayTimes = OUT_DATA.elementTextTrim("VIP_DELAY_TIMES"); String alerDelayTimes = OUT_DATA.elementTextTrim("ALRE_DELAY_TIMES"); response.setPhoneNo(phoneNo); response.setCustName(custName); response.setRunName(runName); response.setCreditName(creditName); response.setCreditCode(creditCode); response.setIdIccid(idIccid); response.setIdAddress(idAddress); response.setVipDelayTimes(vipDelayTimes); response.setAlerDelayTimes(new Integer(alerDelayTimes.equals("")?"0":alerDelayTimes)); } response.setReturnMsg(returnMsg); response.setDetailMsg(detailMsg); response.setPromptMsg(promptMsg); response.setUserMsg(userMsg); return response; } public static void main(String[] args) throws DocumentException { //测试request转为xml S2299QueryRequest request = new S2299QueryRequest(); request.setOpCode("2299"); List<BusiInfo> list = new ArrayList<BusiInfo>(); BusiInfo ba= new BusiInfo(); ba.setIdNo(new Long(1234567890)); BusiInfo ba1= new BusiInfo(); ba1.setIdNo(new Long(987654321)); list.add(ba);list.add(ba1); request.setBusiInfoList(list); System.out.println(MyParseXML.parseS2299QueryRequest2XML(request)); //测试xml转为response对象 StringBuffer response = new StringBuffer(); response.append(xmlHead); response.append("<RETURN_CODE type=\"long\">0</RETURN_CODE>").append("<RETURN_MSG type=\"string\">ok!</RETURN_MSG>") .append("<USER_MSG type=\"string\">ok!</USER_MSG>").append("<DETAIL_MSG type=\"string\">OK!</DETAIL_MSG>") .append("<PROMPT_MSG type=\"string\">").append("</PROMPT_MSG><OUT_DATA>").append("<PHONE_NO type=\"string\">18334518378</PHONE_NO>") .append("<CUST_NAME type=\"string\">秦*</CUST_NAME>").append("<RUN_NAME type=\"string\">正常</RUN_NAME>") .append("<CREDIT_NAME type=\"string\">5星级</CREDIT_NAME>").append("<CREDIT_CODE type=\"string\">09</CREDIT_CODE>") .append("<ID_ICCID type=\"string\">14048119960228244x</ID_ICCID>").append("<ID_ADDRESS type=\"string\">实****************</ID_ADDRESS>") .append("<VIP_DELAY_TIMES type=\"string\">3</VIP_DELAY_TIMES>").append("<ALRE_DELAY_TIMES type=\"int\">0</ALRE_DELAY_TIMES>") .append("</OUT_DATA></ROOT>"); S2299QueryResponse res = parseS2299QueryResponse(response.toString()); StringBuffer outInfo = new StringBuffer(); outInfo.append(res.getCustName()+" "+res.getCreditName()+" "+res.getIdIccid()+" "+res.getIdAddress()); System.out.println(outInfo.toString()); } }