解析SOAP响应XML

前端之家收集整理的这篇文章主要介绍了解析SOAP响应XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
packagecom.soap.util;

importjava.io.ByteArrayInputStream;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;

importjavax.xml.soap.MessageFactory;
importjavax.xml.soap.MimeHeaders;
importjavax.xml.soap.SOAPBody;
importjavax.xml.soap.SOAPElement;
importjavax.xml.soap.SOAPException;
importjavax.xml.soap.SOAPMessage;

/**
*soapXML解析
*@authorRoger
*/
@SuppressWarnings({"unchecked","unused"})
publicclassSoapXmlUtil{
	
	publicstaticvoidmain(String[]args){
		
		StringBufferxml=newStringBuffer();
		xml.append("<?xmlversion=\"1.0\"encoding=\"utf-8\"?>");
		xml.append("<soap:Envelopexmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		xml.append("<soap:Body>");
		xml.append("<GetExchangeGiftRecordResponsexmlns=\"http://www.tech-trans.com.cn/\">");
		xml.append("<GetExchangeGiftRecordResult>");
		xml.append("<Header>");
		xml.append("<ERRCODE>string</ERRCODE>");
		xml.append("<ERRMSG>string</ERRMSG>");
		xml.append("<REQDATE>string</REQDATE>");
		xml.append("<REQTIME>string</REQTIME>");
		xml.append("</Header>");
		xml.append("<DATA>");
		xml.append("<ExchangeGiftRecord>");
		xml.append("<TXDATE>string</TXDATE>");
		xml.append("<GIFTNAME>string</GIFTNAME>");
		xml.append("<GIFTIMG>string</GIFTIMG>");
		xml.append("<BONUS>decimal</BONUS>");
		xml.append("<QTY>decimal</QTY>");
		xml.append("</ExchangeGiftRecord>");
		xml.append("<ExchangeGiftRecord>");
		xml.append("<TXDATE>string</TXDATE>");
		xml.append("<GIFTNAME>string</GIFTNAME>");
		xml.append("<GIFTIMG>string</GIFTIMG>");
		xml.append("<BONUS>decimal</BONUS>");
		xml.append("<QTY>decimal</QTY>");
		xml.append("</ExchangeGiftRecord>");
		xml.append("</DATA>");
		xml.append("</GetExchangeGiftRecordResult>");
		xml.append("</GetExchangeGiftRecordResponse>");
		xml.append("</soap:Body>");
		xml.append("</soap:Envelope>");

		try{
			System.out.println("开始解析xml.....");

			SOAPMessagemsg=formatSoapString(xml.toString());
			SOAPBodybody=msg.getSOAPBody();
			
			Iterator<SOAPElement>iterator=body.getChildElements();
			List<Map<String,String>>list=newArrayList<Map<String,String>>();
			List<Map<String,String>>orgList=printBody(iterator,null,list);
			
		}catch(SOAPExceptione){
			e.printStackTrace();
		}
	}

	publicstaticList<Map<String,String>>printBody(
			Iterator<SOAPElement>iterator,Stringside,List<Map<String,String>>list){
		
		while(iterator.hasNext()){
			SOAPElementelement=iterator.next();
			System.out.println(element.getNodeName());
			
			if("DATA".equals(element.getNodeName())){
				Iterator<SOAPElement>it=element.getChildElements();
				Map<String,String>infoMap=newHashMap<String,String>();
				SOAPElementel=null;
				Stringpk_corp="";//公司主键
				Stringunitcode="";//公司编码
				Stringunitname="";//公司名称
				Stringfathercorp="";//上级公司ID
				Stringts="";//最新更新时间
				Stringcorplevel="";//公司层级
				Stringisseal="";//是否封存
				while(it.hasNext()){
					el=it.next();
					if(el.getNodeName().equals("pk_corp")){
						pk_corp=el.getValue();
						infoMap.put("pk_corp",pk_corp);
					}
					if(el.getNodeName().equals("unitcode")){
						unitcode=el.getValue();
						infoMap.put("unitcode",unitcode);
					}
					if(el.getNodeName().equals("unitname")){
						unitname=el.getValue();
						infoMap.put("unitname",unitname);
					}
					if(el.getNodeName().equals("fathercorp")){
						fathercorp=el.getValue();
						infoMap.put("fathercorp",fathercorp);
					}
					if(el.getNodeName().equals("ts")){
						ts=el.getValue();
						infoMap.put("ts",ts);
					}
					if(el.getNodeName().equals("corplevel")){
						corplevel=el.getValue();
						infoMap.put("corplevel",corplevel);
					}
					if(el.getNodeName().equals("isseal")){
						isseal=el.getValue();
						infoMap.put("isseal",isseal);
					}
				}
				list.add(infoMap);
			}
			if(null==element.getValue()
					&&element.getChildElements().hasNext()){
				printBody(element.getChildElements(),side+"-----",list);
			}
		}
		returnlist;
	}

	/**
	*把soap字符串格式化为SOAPMessage
	*
	*@paramsoapString
	*@return
	*@see[类、类#方法、类#成员]
	*/
	publicstaticSOAPMessageformatSoapString(StringsoapString){
		MessageFactorymsgFactory;
		try{
			msgFactory=MessageFactory.newInstance();
			SOAPMessagereqMsg=msgFactory.createMessage(newMimeHeaders(),newByteArrayInputStream(soapString.getBytes("UTF-8")));
			reqMsg.saveChanges();
			returnreqMsg;
		}catch(Exceptione){
			e.printStackTrace();
			returnnull;
		}
	}

}


备注: 解析的xml标签标签之间不能有空格,否则回去先如下异常

开始解析xml.....
Exceptioninthread"main"java.lang.ClassCastException:com.sun.xml.internal.messaging.saaj.soap.impl.TextImplcannotbecasttojavax.xml.soap.SOAPElement
	atcom.soap.util.SoapXmlUtil.printBody(SoapXmlUtil.java:79)
	atcom.soap.util.SoapXmlUtil.main(SoapXmlUtil.java:67)
原文链接:https://www.f2er.com/xml/295046.html

猜你在找的XML相关文章