调用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; }

 

转载于:https://www.cnblogs.com/jassy/p/10063538.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值