List of usage examples for org.dom4j Element elementIterator
Iterator<Element> elementIterator();
From source file:com.ah.ui.actions.hiveap.HiveApFileAction.java
/** * Get the image product name and version from the header xml string * *@param lineStr -// w w w .j a v a 2 s. c o m *@return HiveApImageInfo */ private HiveApImageInfo getImageInfoHead(String lineStr) { try { if (!lineStr.startsWith("#!/bin/bash") || !lineStr.contains("<Image-Header") || !lineStr.contains("</Image-Header>") || !lineStr.contains("<Firmware>") || !lineStr.contains("</Firmware>")) { return null; } HiveApImageInfo info = new HiveApImageInfo(); SAXReader reader = new SAXReader(); String docName = lineStr.substring(lineStr.indexOf("<Firmware>"), lineStr.indexOf("</Firmware>") + "</Firmware>".length()); Document doc = reader.read(new StringReader(docName)); Element roota = doc.getRootElement(); Iterator<?> iter = roota.elementIterator(); Element foo; while (iter.hasNext()) { foo = (Element) iter.next(); // get product name if (foo.getName().equalsIgnoreCase("Product")) { info.setProductName(foo.getStringValue()); // get image version } else if (foo.getName().equalsIgnoreCase("Version")) { iter = foo.elementIterator(); while (iter.hasNext()) { foo = (Element) iter.next(); if (foo.getName().equalsIgnoreCase("External")) { iter = foo.elementIterator(); while (iter.hasNext()) { foo = (Element) iter.next(); // get major version if (foo.getName().equalsIgnoreCase("Major")) { info.setMajorVersion(foo.getStringValue()); // get minor version } else if (foo.getName().equalsIgnoreCase("Minor")) { info.setMinorVersion(foo.getStringValue()); // get release version } else if (foo.getName().equalsIgnoreCase("Release")) { info.setRelVersion(foo.getStringValue()); // get patch string } else if (foo.getName().equalsIgnoreCase("Patch")) { try { info.setImageUid(Integer.parseInt(foo.getStringValue())); } catch (NumberFormatException nfe) { info.setImageUid(0); } } } } } } } String regex = "^\\d+\\.+\\d+r\\d+\\w*$"; // check the product name and version format if ("".equals(info.getProductName()) || !Pattern.matches(regex, info.getImageVersion().trim())) { return null; } com.ah.be.config.image.ImageManager.updateHiveApImageInfo(info); return info; } catch (Exception ex) { log.error("checkImageInfo : ", ex.getMessage()); return null; } }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static void iterate(Element parentElement, ElementProcess elementProcess) throws DocumentException { // iterate through child elements of element for (Iterator i = parentElement.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); // do something elementProcess.process(element); }//w w w. j a v a2s .c o m }
From source file:com.appdynamics.snmp.SNMPTrapSender.java
License:Apache License
/** * Parses the config xml/*from ww w . ja v a 2 s.c om*/ * @param xml Configuration file locations * @return Map<String, String> - Map of config objects * @throws DocumentException */ private static Map<String, String> parseXML(String xml) throws DocumentException { Map<String, String> map = new HashMap<String, String>(); SAXReader reader = new SAXReader(); Document document = reader.read(xml); Element root = document.getRootElement(); for (Iterator<Element> i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); if (element.getName().equals("snmp-v3")) { Iterator<Element> elementIterator = element.elementIterator(); for (Iterator<Element> j = elementIterator; j.hasNext();) { element = (Element) j.next(); map.put(element.getName(), element.getText()); } } else { map.put(element.getName(), element.getText()); } } return map; }
From source file:com.atguigu.p2p.util.XmlUtils.java
License:Apache License
@SuppressWarnings("unchecked") public static Map<String, String> xml2Map(String xml) { Map<String, String> map = new HashMap<String, String>(); try {//from ww w.j av a2s.c o m Document document = DocumentHelper.parseText(xml); Element rootElement = document.getRootElement(); for (Iterator<Element> it = rootElement.elementIterator(); it.hasNext();) { Element element = (Element) it.next(); map.put(element.getName(), element.getText()); } } catch (Exception e) { logger.error("xml2Map error:", e); } return map; }
From source file:com.atguigu.p2p.util.XmlUtils.java
License:Apache License
@SuppressWarnings("unchecked") public static List<Map<String, String>> xml2MapList(String xml, String xPath) { List<Map<String, String>> mapList = new ArrayList<Map<String, String>>(); try {/*from w w w .j a v a2 s. co m*/ Document document = DocumentHelper.parseText(xml); List<Element> selectNodes = document.selectNodes(xPath); for (Element e : selectNodes) { Map<String, String> map = new HashMap<String, String>(); for (Iterator<Element> it = e.elementIterator(); it.hasNext();) { Element element = (Element) it.next(); map.put(element.getName(), element.getText()); } mapList.add(map); } } catch (Exception e) { logger.error("xml2MapList error:", e); } return mapList; }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * @description xml??map/* w w w . java2s . c o m*/ * @param xml * @return Map */ private HashMap<String, Object> xmlToMap(String xml) { HashMap<String, Object> map = new HashMap<String, Object>(); org.dom4j.Document doc = null; try { doc = org.dom4j.DocumentHelper.parseText(xml); // XML org.dom4j.Element rootElt = doc.getRootElement(); // ? HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for (Iterator i = rootElt.elementIterator(); i.hasNext();) { org.dom4j.Element e = (org.dom4j.Element) i.next(); if ("statusCode".equals(e.getName()) || "statusMsg".equals(e.getName())) map.put(e.getName(), e.getText()); else { if ("SubAccount".equals(e.getName()) || "TemplateSMS".equals(e.getName()) || "totalCount".equals(e.getName()) || "token".equals(e.getName()) || "callSid".equals(e.getName()) || "state".equals(e.getName()) || "downUrl".equals(e.getName())) { if (!"SubAccount".equals(e.getName()) && !"TemplateSMS".equals(e.getName())) { hashMap2.put(e.getName(), e.getText()); } else if ("SubAccount".equals(e.getName())) { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { org.dom4j.Element e2 = (org.dom4j.Element) i2.next(); hashMap3.put(e2.getName(), e2.getText()); } arrayList.add(hashMap3); hashMap2.put("SubAccount", arrayList); } else if ("TemplateSMS".equals(e.getName())) { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { org.dom4j.Element e2 = (org.dom4j.Element) i2.next(); hashMap3.put(e2.getName(), e2.getText()); } arrayList.add(hashMap3); hashMap2.put("TemplateSMS", arrayList); } map.put("data", hashMap2); } else { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { org.dom4j.Element e2 = (org.dom4j.Element) i2.next(); // hashMap2.put(e2.getName(),e2.getText()); hashMap3.put(e2.getName(), e2.getText()); } if (hashMap3.size() != 0) { hashMap2.put(e.getName(), hashMap3); } else { hashMap2.put(e.getName(), e.getText()); } map.put("data", hashMap2); } } } } catch (org.dom4j.DocumentException e) { e.printStackTrace(); logger.error(e.getMessage()); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } return map; }
From source file:com.belle.yitiansystem.merchant.service.impl.MerchantsService.java
@Override public void loadAuthResource(String userId) throws Exception { long enter = System.currentTimeMillis(); Set<String> authrityUrls = null; Document document = XmlTool//from w w w. jav a2 s .co m .createDocument(MerchantsService.class.getClassLoader().getResource("authority.xml").getPath()); Element root = XmlTool.getRootElement(document); Element indexElement = root.element("index"); Element menuEle = null; String key = ""; List<MerchantsAuthorityVo> childAuthorityList = null; Map<String, List<MerchantsAuthorityVo>> _map = null; List<MerchantsAuthorityVo> auths = this.getMerchantAuthorityById(userId); _map = new TreeMap<String, List<MerchantsAuthorityVo>>(new AuthorityComparator()); authrityUrls = new HashSet<String>(); for (MerchantsAuthorityVo _authority : auths) { //parenAuth = merchantsAuthorityDaoImpl.findUniqueBy("id", _authority.getParentId(), false); if (StringUtils.isNotBlank(_authority.getAuthrityURL())) { for (Iterator<Element> it = root.elementIterator(); it.hasNext();) { menuEle = it.next(); if (_authority.getAuthrityURL().indexOf(XmlTool.getAttributeVal(menuEle, "url")) != -1) { for (Iterator<Element> eit = menuEle.elementIterator(); eit.hasNext();) { authrityUrls.add(XmlTool.getAttributeVal(eit.next(), "url")); } break; } } authrityUrls.add("/" + _authority.getAuthrityURL()); } key = _authority.getParentAuthrityName() + "@~" + _authority.getSortNo(); childAuthorityList = _map.get(key); if (childAuthorityList == null) { childAuthorityList = new ArrayList<MerchantsAuthorityVo>(); _map.put(key, childAuthorityList); } childAuthorityList.add(_authority); } for (Iterator<Element> indexIt = indexElement.elementIterator(); indexIt.hasNext();) { authrityUrls.add(XmlTool.getAttributeVal(indexIt.next(), "url")); } Map<String, Set<String>> authReseMap = (Map<String, Set<String>>) redisTemplate.opsForHash() .get(Constant.C_USER_REOURCE_AUTH, "authReourecesMap"); if (authReseMap != null) { authReseMap.remove(userId); authReseMap.put(userId, authrityUrls); redisTemplate.opsForHash().put(Constant.C_USER_REOURCE_AUTH, "authReourecesMap", authReseMap); } Map<String, Map<String, List<MerchantsAuthorityVo>>> aMap = (Map<String, Map<String, List<MerchantsAuthorityVo>>>) redisTemplate .opsForHash().get(Constant.C_USER_AUTH, "authMap"); if (aMap != null) { aMap.remove(userId); aMap.put(userId, _map); redisTemplate.opsForHash().put(Constant.C_USER_AUTH, "authMap", aMap); } //???redis //?? //???????????? //?????? MerchantUser user = merchantUserDaoImpl.findUniqueBy("id", userId, false); CritMap map = new CritMap(); map.addEqual("deleteFlag", 1);// 1 map.addEqual("status", 1); //?? map.addEqual("isAdministrator", 0);// ?? map.addEqual("merchantCode", user.getMerchantCode());// ?? List<MerchantUser> childMerUser = merchantUserDaoImpl.findByCritMap(map, false); for (MerchantUser u : childMerUser) { distributeAuthResourceToChildRen(user.getId(), u.getId()); } System.out.println("======spent time(unit:s):=======" + (System.currentTimeMillis() - enter) / 1000.0); }
From source file:com.belle.yitiansystem.merchant.service.impl.MerchantsService.java
@Override public void delAuthResource(String userId) throws Exception { long enter = System.currentTimeMillis(); Set<String> authrityUrls = null; Document document = XmlTool//from ww w .j av a 2 s . com .createDocument(MerchantsService.class.getClassLoader().getResource("authority.xml").getPath()); Element root = XmlTool.getRootElement(document); Element indexElement = root.element("index"); authrityUrls = new HashSet<String>(); List<MerchantsAuthorityVo> auths = this.getMerchantAuthorityById(userId); for (Iterator<Element> indexIt = indexElement.elementIterator(); indexIt.hasNext();) { authrityUrls.add(XmlTool.getAttributeVal(indexIt.next(), "url")); } Map<String, Set<String>> authReseMap = (Map<String, Set<String>>) redisTemplate.opsForHash() .get(Constant.C_USER_REOURCE_AUTH, "authReourecesMap"); if (authReseMap != null) { authReseMap.remove(userId); authReseMap.put(userId, authrityUrls); redisTemplate.opsForHash().put(Constant.C_USER_REOURCE_AUTH, "authReourecesMap", authReseMap); } Map<String, Map<String, List<MerchantsAuthorityVo>>> aMap = (Map<String, Map<String, List<MerchantsAuthorityVo>>>) redisTemplate .opsForHash().get(Constant.C_USER_AUTH, "authMap"); if (aMap != null) { aMap.remove(userId); aMap.put(userId, new TreeMap<String, List<MerchantsAuthorityVo>>(new AuthorityComparator())); redisTemplate.opsForHash().put(Constant.C_USER_AUTH, "authMap", aMap); } Map<String, Integer> userAuthCountMap = (Map<String, Integer>) redisTemplate.opsForHash() .get(CacheConstant.C_USERS_AUTH_COUNT, "authCount"); if (userAuthCountMap != null) { userAuthCountMap.remove(userId); userAuthCountMap.put(userId, auths != null ? auths.size() : 0); redisTemplate.opsForHash().put(CacheConstant.C_USERS_AUTH_COUNT, "authCount", userAuthCountMap); } //???redis //?? //???????????? //?????? MerchantUser user = merchantUserDaoImpl.findUniqueBy("id", userId, false); CritMap map = new CritMap(); map.addEqual("deleteFlag", 1);// 1 map.addEqual("status", 1); //?? map.addEqual("isAdministrator", 0);// ?? map.addEqual("merchantCode", user.getMerchantCode());// ?? List<MerchantUser> childMerUser = merchantUserDaoImpl.findByCritMap(map, false); for (MerchantUser u : childMerUser) { distributeAuthResourceToChildRen(user.getId(), u.getId()); } System.out.println("======spent time(unit:s):=======" + (System.currentTimeMillis() - enter) / 1000.0); }
From source file:com.bizideal.whoami.utils.cloopen.CCPRestSmsSDK.java
License:Open Source License
/** * @description xml??map/* ww w .j a va2 s. c o m*/ * @param xml * @return Map */ private HashMap<String, Object> xmlToMap(String xml) { HashMap<String, Object> map = new HashMap<String, Object>(); Document doc = null; try { doc = DocumentHelper.parseText(xml); // XML Element rootElt = doc.getRootElement(); // ? HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); for (Iterator i = rootElt.elementIterator(); i.hasNext();) { Element e = (Element) i.next(); if ("statusCode".equals(e.getName()) || "statusMsg".equals(e.getName())) map.put(e.getName(), e.getText()); else { if ("SubAccount".equals(e.getName()) || "totalCount".equals(e.getName()) || "token".equals(e.getName()) || "downUrl".equals(e.getName())) { if (!"SubAccount".equals(e.getName())) { hashMap2.put(e.getName(), e.getText()); } else { ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { Element e2 = (Element) i2.next(); hashMap3.put(e2.getName(), e2.getText()); arrayList.add(hashMap3); } hashMap2.put("SubAccount", arrayList); } map.put("data", hashMap2); } else { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { Element e2 = (Element) i2.next(); // hashMap2.put(e2.getName(),e2.getText()); hashMap3.put(e2.getName(), e2.getText()); } if (hashMap3.size() != 0) { hashMap2.put(e.getName(), hashMap3); } else { hashMap2.put(e.getName(), e.getText()); } map.put("data", hashMap2); } } } } catch (DocumentException e) { e.printStackTrace(); LoggerUtil.error(e.getMessage()); } catch (Exception e) { LoggerUtil.error(e.getMessage()); e.printStackTrace(); } return map; }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * ?Element???//from ww w.j a v a 2 s.co m * * * * * * @param element * Element * @return String */ public static String getElementContext(Element element) { if (element == null) { return null; } String str = element.getText(); Element tmp = null; for (Iterator i = element.elementIterator(); i.hasNext();) { tmp = (Element) i.next(); str = str + tmp.asXML(); // do something } return str; }