调用webservice,解析xml属性值

1、调用webservice

try {
            String endpoint = "http://22.222.22.22:280/iss-ws/services/SyncUserInfo?wsdl";
            //直接引用远程的wsdl文件
            //以下都是套路 
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(endpoint);
            call.setOperationName("getUserForAll");//WSDL里面描述的接口名称
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 
            String xmlString = (String)call.invoke(new Object[]{});

            JSONObject json = getXmlAttribute(xmlString,"shujuziyuan");
            System.out.println(json.get("code"));
            System.out.println(json.get("name"));
            System.out.println(json.get("orgId"));
        }
            
            
            
            catch (Exception e) {
            System.err.println(e.toString());
            }

2、解析xml字符串

//xml格式为
/*

<?xml version="1.0" encoding="UTF-8"?>
<hnhb>
<Users>
<User id="001489dab49841a590f73af5b20691a0" code="xgs2" name="纸业有限公司" orgId="" areaId="" email="" tel="" status="1" type="2" createDate="2018-11-10 17:04:58" updateDate="2018-11-10 17:04:58"></User>
<User id="002c49abd2cf46b6be2f" code="liufushan" name="测试测试" orgId="0" areaId="" email="" tel="" status="1" type="" createDate="2018-11-27 00:00:00" updateDate="2018-11-27 00:00:00"></User>
</Users>
<header><rtnCode>000000</rtnCode><rtnMess></rtnMess></header>
</hnhb>

*/


public
static JSONObject getXmlAttribute(String xml,String username) { Document doc = null; JSONObject jsonObject= new JSONObject(); try { // 将字符串转为XML doc = DocumentHelper.parseText(xml); // 获取根节点 Element rootElt = doc.getRootElement(); //获取城市名 Iterator ABK = rootElt.elementIterator("Users"); while (ABK.hasNext()) { Element abkRecord = (Element) ABK.next(); //获取ABK节点下的所有节点 Iterator f = abkRecord.elementIterator(); while (f.hasNext()) { Element itemAtr = (Element) f.next(); //获取需要的数据 if(username.equals(itemAtr.attributeValue("code"))){ jsonObject.put("id", itemAtr.attributeValue("id")); jsonObject.put("code", itemAtr.attributeValue("code")); jsonObject.put("name", itemAtr.attributeValue("name")); jsonObject.put("orgId", itemAtr.attributeValue("orgId")); jsonObject.put("areaId", itemAtr.attributeValue("areaId")); jsonObject.put("email", itemAtr.attributeValue("email")); jsonObject.put("tel", itemAtr.attributeValue("tel")); jsonObject.put("status", itemAtr.attributeValue("status")); jsonObject.put("type", itemAtr.attributeValue("type")); jsonObject.put("createDate", itemAtr.attributeValue("createDate")); jsonObject.put("updateDate", itemAtr.attributeValue("updateDate")); break; } } } } catch (DocumentException e) { e.printStackTrace(); } return jsonObject; }

 

posted @ 2018-12-04 13:07  jassy  阅读(3735)  评论(0编辑  收藏  举报