1.下载,引入dom4j jar包
2.
SAXReader reader = new SAXReader(); try { Document doc = reader.read(new File("tree.xml")); //获取根节点 Element root = doc.getRootElement(); //遍历根节点的子节点 Iterator<Element> it = root.elementIterator(); while (it.hasNext()) { Element ele = it.next(); List<Attribute> attrList = ele.attributes(); for (Attribute attr : attrList) { System.out.println("属性名:"+attr.getName()); System.out.println("属性值:"+attr.getValue()); } //遍历当前节点的子节点 Iterator<Element> childit = ele.elementIterator(); while (childit.hasNext()) { Element child = childit.next(); System.out.println("节点名:"+child.getName()); System.out.println("节点值:"+child.getStringValue()); } } } catch (DocumentException e) { e.printStackTrace(); }
时间: 2024-10-21 05:06:49