使用dom4j解析XML例子
包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), TestDom4jReadExmple.java(测试解析的结果)
代码运行前需先导入dom4j架包。
studentInfo.xml文件(该文件放在本项目目录下)内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version= "1.0" encoding= "gb2312" ?> <students> <student age= "25" ><!--如果没有age属性,默认的为 20 --> <name>崔卫兵</name> <college>PC学院</college> <telephone> 62354666 </telephone> <notes>男, 1982 年生,硕士,现就读于北京邮电大学</notes> </student> <student> <name>cwb</name> <college leader= "学院领导" >PC学院</college><!--如果没有leader属性,默认的为leader--> <telephone> 62358888 </telephone> <notes>男, 1987 年生,硕士,现就读于中国农业大学</notes> </student> <student age= "45" > <name>xxxxx</name> <college leader= "" >xxx学院</college> <telephone> 66666666 </telephone> <notes>注视中,注释中</notes> </student> <student age= "" > <name>lxx</name> <college>yyyy学院</college> <telephone> 88888888 </telephone> <notes>注视中 111 ,注释中 222 </notes> </student> </students> |
Dom4jReadExmple.java类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import java.io.File; import java.util.HashMap; import java.util.Iterator; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * 利用dom4j进行XML编程 * * @author henuyuxiang * @since 2014.7.11 */ public class Dom4jReadExmple { /** * 遍历整个XML文件,获取所有节点的值与其属性的值,并放入HashMap中 * * @param filename * String 待遍历的XML文件(相对路径或者绝对路径) * @param hm * HashMap * 存放遍历结果,格式:<nodename,nodevalue>或者<nodename+attrname,attrvalue> */ public void iterateWholeXML(String filename, HashMap<String, String> hm) { SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read( new File(filename)); Element root = document.getRootElement(); // 用于记录学生编号的变量 int num = - 1 ; // 遍历根结点(students)的所有孩子节点(肯定是student节点) for ( @SuppressWarnings ( "rawtypes" ) Iterator iter = root.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); num++; // 获取person节点的age属性的值 Attribute ageAttr = element.attribute( "age" ); if (ageAttr != null ) { String age = ageAttr.getValue(); if (age != null && !age.equals( "" )) { hm.put(element.getName() + "-" + ageAttr.getName() + num, age); } else { hm.put(element.getName() + "-" + ageAttr.getName() + num, "20" ); } } else { hm.put(element.getName() + "-age" + num, "20" ); } // 遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理 for ( @SuppressWarnings ( "rawtypes" ) Iterator iterInner = element.elementIterator(); iterInner .hasNext();) { Element elementInner = (Element) iterInner.next(); if (elementInner.getName().equals( "college" )) { hm.put(elementInner.getName() + num, elementInner.getText()); // 获取college节点的leader属性的值 Attribute leaderAttr = elementInner.attribute( "leader" ); if (leaderAttr != null ) { String leader = leaderAttr.getValue(); if (leader != null && !leader.equals( "" )) { hm.put(elementInner.getName() + "-" + leaderAttr.getName() + num, leader); } else { hm.put(elementInner.getName() + "-" + leaderAttr.getName() + num, "leader" ); } } else { hm.put(elementInner.getName() + "-leader" + num, "leader" ); } } else { hm.put(elementInner.getName() + num, elementInner.getText()); } } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
TestDom4jReadExmple.java类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package demo1; import java.util.HashMap; /** * 测试Dom4jReadExmple解析的情况 * * @author henuyuxiang * @since 2014.7.11 */ public class TestDom4jReadExmple { public static void main(String[] args) { try { // 获取解析完后的解析信息 HashMap<String, String> hashMap; Dom4jReadExmple drb = new Dom4jReadExmple(); // 遍历整个XML文件 hashMap = new HashMap<String, String>(); drb.iterateWholeXML( "studentInfo.xml" , hashMap); System.out.println( "姓名\t年龄\t学院\t学院领导\t电话\t备注" ); for ( int i = 0 ; i < hashMap.size(); i += 6 ) { int j = i / 6 ; System.out.print(hashMap.get( "name" + j) + "\t" ); System.out.print(hashMap.get( "student-age" + j) + "\t" ); System.out.print(hashMap.get( "college" + j) + "\t" ); System.out.print(hashMap.get( "college-leader" + j) + "\t" ); System.out.print(hashMap.get( "telephone" + j) + "\t" ); System.out.println(hashMap.get( "notes" + j) + "\t" ); } } catch (Exception ex) { ex.printStackTrace(); } } } |
运行结果如下图所示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix