zoukankan      html  css  js  c++  java
  • 调用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; }
  • 相关阅读:
    到底该不该熟悉掌握struts2的ONGL呢?
    struts2 request内幕 为什么在struts2用EL表达式可以取值
    struts2 权限拦截器 拦截没有登陆的请求
    tomcat context 配置 项目部署
    tomcat 设置默认编码格式
    工作记录1
    javascript 的学习笔记(第一天)
    JavaScript for...in 循环
    indexof方法区分大小写
    java 和 IntelliJ IDEA 的一些配置
  • 原文地址:https://www.cnblogs.com/jassy/p/10063538.html
Copyright © 2011-2022 走看看