最近接了一家供應商,他們的服務端採用webservice,但只能通過http進行請求,請求的返回值是如下這種格式:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<res:Response state="success" ID="myProduct">
<res:List HotelId="888888" tip="不能取消" dateTime="2019-02-20 09:53:46">
<res:Tests>
<res:Test name="大牀房" currency="CNY" code="123456" productName="豪華大牀房" roomId="666666">
<res:Elements>
<res:Elements price="200" date="2019-02-20"/>
<res:Elements price="225" date="2019-02-21"/>
</res:Elements>
</res:Test>
<res:Test name="雙牀房" currency="CNY" code="654321" productName="豪華雙牀房" roomId="333333">
<res:Elements>
<res:Elements price="400" date="2019-02-20"/>
<res:Elements price="500" date="2019-02-21"/>
</res:Elements>
</res:Test>
</res:Tests>
</res:List>
</res:Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
網上搜了一下解析方式,如下:
MessageFactory messageFactory;
messageFactory = MessageFactory.newInstance();
SOAPMessage reqMsg = messageFactory.createMessage(new MimeHeaders(),new ByteArrayInputStream(soapString.getBytes("UTF-8")));
reqMsg.saveChanges();
SOAPBody body = reqMsg.getSOAPBody();
Iterator<SOAPElement> iterator = body.getChildElements();
while (iterator.hasNext()) {
SOAPElement element = (SOAPElement) iterator.next();
if ("res:Response".equals(element.getNodeName())) {
String status = element.getAttribute("Status");
if ("success".equals(status)) {
Iterator<SOAPElement> childIterator = element.getChildElements();
String HotelId=element.getAttribute("HotelId"));
while (childIterator.hasNext()) {
}
}
}
}
大致就是先將返回值解析爲 SOAPBody,然後對 SOAPBody 裏面的子元素循環迭代取出來需要的字段值解析。這是最簡單,快速的解決方案。略微思考一下發現,可維護行非常差,而且代碼非常冗餘,於是經過反覆的修改的與驗證,提取出瞭如下的工具類:
import javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.util.*;
public class SoapUtil {
/**
* 將webservice 的返回值各個元素存儲到map中
* @param res
* @return
*/
public static Map<String,Object> doSoapToXml(String res){
if(res==null) return null;
Map<String,Object> map=new HashMap<>();
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage reqMsg = messageFactory.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(res.getBytes("UTF-8")));
reqMsg.saveChanges();
SOAPBody soapBody= reqMsg.getSOAPBody();
serialXml(soapBody,map);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return map;
}
/**
* 將soap中獲取的元素轉爲list
* @return
*/
public static List<SOAPElement> soap2List(Map<String,Object> map,String elementName) {
List<SOAPElement> soapElementList=new ArrayList<>();
Object obj=map.get(elementName);
if(obj==null)return soapElementList;
if(obj instanceof SOAPElement){
SOAPElement picture= (SOAPElement) obj;
soapElementList.add(picture);
}else if(obj instanceof List){
List<SOAPElement> pictures= (List<SOAPElement>) obj;
soapElementList.addAll(pictures);
}
return soapElementList;
}
/**
* 將 SOAPElement 中的元素若只有一個,則存儲value值爲單個的SOAPElement對象,若有多個相同的元素,則存儲爲 List<SOAPElement> 對象
* @param soapElement
* @param map
*/
public static void serialXml(SOAPElement soapElement, Map<String,Object> map){
String nodeName=soapElement.getNodeName();
if(map.containsKey(nodeName)){
List<SOAPElement> list=new ArrayList<>();
list.add(soapElement);
Object obj=map.get(nodeName);
if(obj instanceof SOAPElement){
SOAPElement origSOAPElement= (SOAPElement) map.get(nodeName);
list.add(origSOAPElement);
}else if(obj instanceof List){
List<SOAPElement> origList= (List<SOAPElement>) map.get(nodeName);
list.addAll(origList);
}
map.put(nodeName,list);
}else{
map.put(nodeName,soapElement);
}
Iterator iterator=soapElement.getChildElements();
if(iterator.hasNext()){
while (iterator.hasNext()){
Object obj=iterator.next();
if(obj instanceof SOAPElement){
SOAPElement childSoapElement= (SOAPElement) obj;
serialXml(childSoapElement,map);
}
}
}
}
}
整個工具類形成的思路就是將 SOAPBody 中的各個元素通過遞歸的方式,nodeName 節點名作爲key, 節點所對應的SOAPElement 元素 作爲value存儲到Map中,若是單個節點,就存儲爲對象。若是節點數組,則存儲爲 List對象。
soap2List 方法方便從Map 中取出對應的數組元素,有可能此數組元素只有一個,所以統一轉爲LIst中存儲。
最後發散一下,我們在處理類似這種在元素中循環迭代的問題時,都可以採用遞歸的方式將其抽象出來,這樣在代碼中就能專注業務邏輯,同時我們的代碼質量也會變得更高