package com.soap.util; import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; /** * soap XML解析 * @author Roger */ @SuppressWarnings({"unchecked","unused"}) public class SoapXmlUtil { public static void main(String[] args) { StringBuffer xml = new StringBuffer(); xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); xml.append("<soap:Envelope xmlns: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("<GetExchangeGiftRecordResponse xmlns=\"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....."); SOAPMessage msg = formatSoapString(xml.toString()); SOAPBody body = msg.getSOAPBody(); Iterator<SOAPElement> iterator = body.getChildElements(); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); List<Map<String, String>> orgList = printBody(iterator, null, list); } catch (SOAPException e) { e.printStackTrace(); } } public static List<Map<String, String>> printBody( Iterator<SOAPElement> iterator, String side, List<Map<String, String>> list) { while (iterator.hasNext()) { SOAPElement element = iterator.next(); System.out.println(element.getNodeName()); if ("DATA".equals(element.getNodeName())) { Iterator<SOAPElement> it = element.getChildElements(); Map<String, String> infoMap = new HashMap<String, String>(); SOAPElement el = null; String pk_corp = ""; // 公司主键 String unitcode = ""; // 公司编码 String unitname = ""; // 公司名称 String fathercorp = ""; // 上级公司ID String ts = ""; // 最新更新时间 String corplevel = ""; // 公司层级 String isseal = ""; // 是否封存 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); } } return list; } /** * 把soap字符串格式化为SOAPMessage * * @param soapString * @return * @see [类、类#方法、类#成员] */ public static SOAPMessage formatSoapString(String soapString) { MessageFactory msgFactory; try { msgFactory = MessageFactory.newInstance(); SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(), new ByteArrayInputStream(soapString.getBytes("UTF-8"))); reqMsg.saveChanges(); return reqMsg; } catch (Exception e) { e.printStackTrace(); return null; } } }
备注: 解析的xml标签与标签之间不能有空格,否则回去先如下异常
开始解析 xml..... Exception in thread "main" java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to javax.xml.soap.SOAPElement at com.soap.util.SoapXmlUtil.printBody(SoapXmlUtil.java:79) at com.soap.util.SoapXmlUtil.main(SoapXmlUtil.java:67)