1. 程式人生 > >axis方式呼叫WebService介面、xml轉json

axis方式呼叫WebService介面、xml轉json

axis方式呼叫WebService介面

xml轉json

一、引入pom檔案或jar包

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>

二、寫呼叫工具類

    /**
     * axis方式訪問webservice
     * 
     * @param dataMap
     *            請求的xml的報文
     * @param endPoint
     *            webservice的wsdl地址,例如:http://localhost:8080/project/services/mainName?wsdl
     * @param namespace
     *            呼叫方法的名稱空間
     * @param method
     *            呼叫的方法名
     * @return 返回呼叫的報文(xml)
     * 
     */
public static String invokeByAxis(Map<String,Object> dataMap, String endPoint, String namespace, String method) { String responseXML = ""; Service service = new Service(); try { Call call = (Call) service.createCall(); //設定請求超時時間 call
.setTimeout(5000); //webservice介面地址 call.setTargetEndpointAddress(endPoint); //請求方法 call.setOperationName(method); //請求名稱空間 call.setOperationName(new javax.xml.namespace.QName(namespace, method)); //是否是soapaction call
.setUseSOAPAction(true); //封裝請求引數 Set<String> set = dataMap.keySet(); List<Object> objParams = new ArrayList<>(); for(Iterator<String> it=set.iterator(); it.hasNext();){ String key = it.next(); Object value = dataMap.get(key); objParams.add(value); call.addParameter(key, org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); } call.setReturnType(XMLType.XSD_STRING); responseXML = (String) call.invoke(objParams.toArray()); } catch (Exception e) { logger.error("WebService請求異常!原因: "+e.fillInStackTrace()); } return responseXML; }

三、xml轉json

    /**
     * xml轉json
     * @param xml
     * @param jsonType
     * @return
     */
    public static  String xmlToJSON(String xml,String charset) {  
        JSONObject obj = new JSONObject();  
        try {  
            InputStream is = new ByteArrayInputStream(xml.getBytes(charset));  
            SAXBuilder sb = new SAXBuilder();  
            Document doc = sb.build(is);  
            Element root = doc.getRootElement();  
            Map map=iterateElement(root);  
           obj.put(root.getName(),map);  
           return obj.toString();  
        } catch (Exception e) {  
            logger.error("xml轉json異常!xml為:"+xml+"-------原因:"+e.fillInStackTrace());
            return null;
        }  
    }  

  private static Map  iterateElement(Element root) {
        List childrenList = root.getChildren();
        Element element = null;
        Map map = new HashMap();
        List list = null;
        for (int i = 0; i < childrenList.size(); i++) {
            list = new ArrayList();
            element = (Element) childrenList.get(i);
            if(element.getChildren().size()>0){
                if(root.getChildren(element.getName()).size()>1){
                    if (map.containsKey(element.getName())) {
                        list = (List) map.get(element.getName());
                    }
                    list.add(iterateElement(element));
                    map.put(element.getName(), list);
                }else{
                    map.put(element.getName(), iterateElement(element));
                }
            }else {
                if(root.getChildren(element.getName()).size()>1){
                    if (map.containsKey(element.getName())) {
                        list = (List) map.get(element.getName());
                    }
                    list.add(element.getTextTrim());
                    map.put(element.getName(), list);
                }else{
                    map.put(element.getName(), element.getTextTrim());
                }
            }
        }

        return map;
    }