1. 程式人生 > >調用webservice,解析xml屬性值

調用webservice,解析xml屬性值

stack null mls als cep ext XML per exc

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

調用webservice,解析xml屬性值