public static void main(String[] args) throws DocumentException { InputStream resourceAsStream = MyDOM4J.class.getClassLoader().getResourceAsStream("test1.xml"); SAXReader saxReader = new SAXReader(); Document read = saxReader.read(resourceAsStream); Element rootElement = read.getRootElement(); System.out.println("当前节点名称 :"+rootElement.getName()); listNodes(rootElement); } //遍历当前节点下的所有节点 public static void listNodes(Element node){ System.out.println("当前节点的名称:" + node.getName()); //首先获取当前节点的所有属性节点 List<Attribute> list = node.attributes(); //遍历属性节点 for(Attribute attribute : list){ System.out.println("属性"+attribute.getName() +":" + attribute.getValue()); } //如果当前节点内容不为空,则输出 if(!(node.getTextTrim().equals(""))){ System.out.println( node.getName() + ":" + node.getText()); } //同时迭代当前节点下面的所有子节点 //使用递归 Iterator<Element> iterator = node.elementIterator(); while(iterator.hasNext()){ Element e = iterator.next(); listNodes(e); } }
@SKS_Annotation(cmd = "queryProgrammeByXML") public static MapResponse SKS_queryProgrammeByXML(SkSession session, DEV_PROGRAM req){ MapResponse res = new MapResponse(); SkTransaction tran = NpDaoFactory.getInstance().openTransaction(); ProgrammeDao programmeDao = tran.getDao(ProgrammeDao.class); try { DEV_PROGRAM programme = programmeDao.queryProgramme(req); res.setData("programme",programme); res.setResult("OK"); //创建SAXReader对象 SAXReader reader = new SAXReader(); //读取文件 转换成Document Document document = reader.read(new File("D:/NovaPluto/xml/"+req.getXML()+".xml")); //获取根节点元素对象 Element root = document.getRootElement(); listmap.clear(); listNodes(root); List<Map<String, Map<String, String>>> lmap = new ArrayList<Map<String, Map<String, String>>>(); if(listmap != null && listmap.size()>0){ for(int i=0;i<listmap.size();i++){ Map<String, Map<String, String>> tmp = listmap.get(i); Map<String, String> ele = null; Map<String, Map<String, String>> smap = new HashMap<String, Map<String, String>>(); for (String key : tmp.keySet()) { ele = new HashMap<String, String>(); if(key.equals("image")){ ele.putAll(listmap.get(i+1).get("effect")); ele.putAll(listmap.get(i+2).get("file")); smap.put("image", ele); lmap.add(smap); } if(key.equals("video")){ ele.putAll(listmap.get(i+1).get("file")); smap.put("video", ele); lmap.add(smap); } if(key.equals("text")){ ele.putAll(listmap.get(i+1).get("singleMode")); ele.putAll(listmap.get(i+2).get("pageCount")); ele.putAll(listmap.get(i+3).get("continuousMove")); ele.putAll(listmap.get(i+4).get("file")); smap.put("text", ele); lmap.add(smap); } System.out.println("key= "+ key + " and value= " + tmp.get(key)); } } } } catch (Exception e) { e.printStackTrace(); res.setResult("NG"); res.setReason(e.toString()); } finally{ tran.close(); } return res; } static List<Map<String, Map<String, String>>> listmap = new ArrayList<Map<String, Map<String, String>>>(); public static void listNodes(Element node){ System.out.println("当前节点的名称:" + node.getName()); //首先获取当前节点的所有属性节点 List<Attribute> list = node.attributes(); //遍历属性节点 Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>(); Map<String, String> map_ = new HashMap<String, String>(); for(Attribute attribute : list){ System.out.println("属性"+attribute.getName() +":" + attribute.getValue()); map_.put(attribute.getName(), attribute.getValue()); } //如果当前节点内容不为空,则输出 if(!(node.getTextTrim().equals(""))){ System.out.println( node.getName() + ":" + node.getText()); map_.put(node.getName(), node.getText()); } map.put(node.getName(), map_); listmap.add(map); //同时迭代当前节点下面的所有子节点 //使用递归 Iterator<Element> iterator = node.elementIterator(); while(iterator.hasNext()){ Element e = iterator.next(); listNodes(e); } }