<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 package dom4jExample.read; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * 利用dom4j与XPath进行XML编程 * @author cuiweibing * @since 2007.8.10 */ public class Dom4jReadExmple { /** * 利用XPath操作XML文件,获取指定节点或者属性的值,并放入HashMap中 * @param filename String 待操作的XML文件(相对路径或者绝对路径) * @param hm HashMap 存放选择的结果,格式:<nodename,nodevalue>或者<nodename+attrname,attrvalue> */ public void getSelectedNodeValue(String filename,HashMap<String,String> hm){ try { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File(filename)); //获取学生姓名为"崔卫兵"的年龄 List list = document.selectNodes("/students/student[name=\"崔卫兵\"]/@age"); Iterator iter = list.iterator(); if (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); hm.put("崔卫兵-"+attribute.getName(),attribute.getValue()); }else{ hm.put("崔卫兵-age","20"); } //获取学生姓名为"崔卫兵"的年龄 list = document.selectNodes("/students/student[name=\"cwb\"]/@age"); iter = list.iterator(); if (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); hm.put("cwb-"+attribute.getName(),attribute.getValue()); }else{ hm.put("cwb-age","20"); } //获取学生姓名为"cwb"所在的学院名称 list = document.selectNodes("/students/student[name=\"cwb\"]/college"); iter = list.iterator(); if (iter.hasNext()) { Element element = (Element) iter.next(); String name = element.getName(); String value = element.getText(); hm.put("cwb-"+name,value); } //获取学生姓名为"cwb"所在学院的领导 list = document.selectNodes("/students/student[name=\"cwb\"]/college/@leader"); iter = list.iterator(); if (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); hm.put("cwb-college-"+attribute.getName(),attribute.getValue()); }else{ hm.put("cwb-college-leader","leader"); } //获取学生姓名为"lxx"所在的学院名称 list = document.selectNodes("/students/student[name=\"lxx\"]/college"); iter = list.iterator(); if (iter.hasNext()) { Element element = (Element) iter.next(); String name = element.getName(); String value = element.getText(); hm.put("lxx-"+name,value); } //获取学生姓名为"lxx"所在学院的领导 list = document.selectNodes("/students/student[name=\"lxx\"]/college/@leader"); iter = list.iterator(); if (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); hm.put("lxx-college-"+attribute.getName(),attribute.getValue()); }else{ hm.put("lxx-college-leader","leader"); } } catch (Exception ex) { ex.printStackTrace(); } } } TestDom4jReadExmple.java package dom4jExample.read; import java.util.HashMap; /** * 测试Dom4jReadExmple解析的情况 * @author cuiweibing * @since 2007.8.10 */ public class TestDom4jReadExmple { public static void main(String[] args) { try{ //获取解析完后的解析信息 HashMap<String,String> hashMap; Dom4jReadExmple drb=new Dom4jReadExmple(); //利用XPath操作XML文件,获取想要的属性值 hashMap = new HashMap<String,String>(); drb.getSelectedNodeValue("studentInfo.xml",hashMap); System.out.println("崔卫兵-age:"+hashMap.get("崔卫兵-age")); System.out.println("cwb-age:"+hashMap.get("cwb-age")); System.out.println("cwb-college:"+hashMap.get("cwb-college")); System.out.println("cwb-college-leader:"+hashMap.get("cwb-college-leader")); System.out.println("lxx-college:"+hashMap.get("lxx-college")); System.out.println("lxx-college-leader:"+hashMap.get("lxx-college-leader")); }catch(Exception ex){ ex.printStackTrace(); } } } 运行结果 崔卫兵-age:25 cwb-age:20 cwb-college:PC学院 cwb-college-leader:学院领导 lxx-college:yyyy学院 lxx-college-leader:leader |