解析xml实例,包含复杂xml的循环解析
前端之家收集整理的这篇文章主要介绍了
解析xml实例,包含复杂xml的循环解析,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Document doc; try { doc = DocumentHelper.parseText(xml); Element root = doc.getRootElement(); Element Action_Ele = root.element("Action"); Element Data_Ele = Action_Ele.element("Data"); Element Task_Ele = Data_Ele.element("Task"); Element Targets_Ele = Task_Ele.element("Targets"); List<Element> Target_Ele_List = Targets_Ele.elements("Target"); if(Target_Ele_List.size()>0){ for(int i=0; i<Target_Ele_List.size();i++){ Element Target_Ele = Target_Ele_List.get(i); String ip = Target_Ele.element("IpRanges").getTextTrim(); String user = Target_Ele.element("User").getTextTrim(); String TargetType = Target_Ele.element("TargetType").getTextTrim(); String type = null; if(TargetType.equals("BUSSINESS")){ if(user.contains("root")){ type = "unix"; }else if(user.contains("administrator")){ type = "win"; }else{ type = "db"; } SsoInterface ssoIntf = new SsoInterfaceImpl(); String pwd = ssoIntf.gettAccPassWord(ip,type,user); if(pwd == null){ return getDeviceReturnXml(ResultCode.RESULT_TARGET_NOT_IN_4A,ip,user); }else{ Target_Ele.element("Password").setText(pwd); if(Target_Ele.element("Jumphosts").hasContent()){ Element Jumphosts_Ele =Target_Ele.element("Jumphosts"); if(Jumphosts_Ele.element("Jumphost").hasContent()){ Element Jumphost_Ele = Jumphosts_Ele.element("Jumphost"); String jump_IP = Jumphost_Ele.element("Ip").getTextTrim(); String jump_user = Jumphost_Ele.element("User").getTextTrim(); String jump_Type = null; if(jump_user.contains("root")){ jump_Type = "unix"; }else if(jump_user.contains("administrator")){ jump_Type = "win"; } String Jump_pwd = ssoIntf.gettAccPassWord(jump_IP,jump_Type,jump_user); ssoIntf.modifyPassWd(jump_IP,jump_user); if(Jump_pwd == null){ return getDeviceReturnXml(ResultCode.RESULT_TARGET_NOT_IN_4A,jump_IP,jump_user); }else{ Jumphost_Ele.element("Password").setText(Jump_pwd); } } } //解析模板 Templates if(Target_Ele.element("Templates").hasContent()){ Element Templates_Ele =Target_Ele.element("Templates"); if(Templates_Ele.element("Template").hasContent()){ for(Iterator i_Tep = Templates_Ele.elementIterator();i_Tep.hasNext();){ Element Template_Ele = (Element) i_Tep.next(); System.out.println(Template_Ele.element("Uuid").getTextTrim()); } } } return doc.asXML(); } } } } } catch (DocumentException e) { e.printStackTrace(); } return xml;