studentInfo.xml
1 <?xml version="1.0" encoding="gb2312"?> 2 <students> 3 <student age="25"><!--如果没有age属性,默认的为20--> 4 <name>崔卫兵</name> 5 <college>PC学院</college> 6 <telephone>62354666</telephone> 7 <notes>男,1982年生,硕士,现就读于北京邮电大学</notes> 8 </student> 9 <student> 10 <name>cwb</name> 11 <college leader="学院领导">PC学院</college><!--如果没有leader属性,默认的为leader--> 12 <telephone>62358888</telephone> 13 <notes>男,1987年生,硕士,现就读于中国农业大学</notes> 14 </student> 15 <student age="45"> 16 <name>xxxxx</name> 17 <college leader="">xxx学院</college> 18 <telephone>66666666</telephone> 19 <notes>注视中,注释中</notes> 20 </student> 21 <student age=""> 22 <name>lxx</name> 23 <college>yyyy学院</college> 24 <telephone>88888888</telephone> 25 <notes>注视中111,注释中222</notes> 26 </student> 27 </students>
Dom4jReadExmple.java
1 package dom4jExample.read; 2 3 import org.dom4j.Attribute; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentException; 6 import org.dom4j.Element; 7 8 import org.dom4j.io.SAXReader; 9 10 import java.io.File; 11 12 import java.util.HashMap; 13 import java.util.Iterator; 14 15 16 /** 17 * 利用dom4j进行XML编程 18 * @author cuiweibing 19 * @since 2007.8.10 20 */ 21 public class Dom4jReadExmple { 22 /** 23 * 遍历整个XML文件,获取所有节点的值与其属性的值,并放入HashMap中 24 * @param filename String 待遍历的XML文件(相对路径或者绝对路径) 25 * @param hm HashMap 存放遍历结果,格式:<nodename,nodevalue>或者<nodename+attrname,attrvalue> 26 */ 27 public void iterateWholeXML(String filename, HashMap<String, String> hm) { 28 SAXReader saxReader = new SAXReader(); 29 30 try { 31 Document document = saxReader.read(new File(filename)); 32 Element root = document.getRootElement(); 33 34 // 用于记录学生编号的变量 35 int num = -1; 36 37 //遍历根结点(students)的所有孩子节点(肯定是student节点) 38 for (Iterator iter = root.elementIterator(); iter.hasNext();) { 39 Element element = (Element) iter.next(); 40 num++; 41 42 // 获取person节点的age属性的值 43 Attribute ageAttr = element.attribute("age"); 44 45 if (ageAttr != null) { 46 String age = ageAttr.getValue(); 47 48 if ((age != null) && !age.equals("")) { 49 hm.put(element.getName() + "-" + ageAttr.getName() + 50 num, age); 51 } else { 52 hm.put(element.getName() + "-" + ageAttr.getName() + 53 num, "20"); 54 } 55 } else { 56 hm.put(element.getName() + "-age" + num, "20"); 57 } 58 59 //遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理 60 for (Iterator iterInner = element.elementIterator(); 61 iterInner.hasNext();) { 62 Element elementInner = (Element) iterInner.next(); 63 64 if (elementInner.getName().equals("college")) { 65 hm.put(elementInner.getName() + num, 66 elementInner.getText()); 67 68 //获取college节点的leader属性的值 69 Attribute leaderAttr = elementInner.attribute("leader"); 70 71 if (leaderAttr != null) { 72 String leader = leaderAttr.getValue(); 73 74 if ((leader != null) && !leader.equals("")) { 75 hm.put(elementInner.getName() + "-" + 76 leaderAttr.getName() + num, leader); 77 } else { 78 hm.put(elementInner.getName() + "-" + 79 leaderAttr.getName() + num, "leader"); 80 } 81 } else { 82 hm.put(elementInner.getName() + "-leader" + num, 83 "leader"); 84 } 85 } else { 86 hm.put(elementInner.getName() + num, 87 elementInner.getText()); 88 } 89 } 90 } 91 } catch (DocumentException e) { 92 // TODO Auto-generated catch block 93 e.printStackTrace(); 94 } 95 } 96 }
http://www.kuaipan.cn/file/id_32645732884309089.htm