class="java" name="code"> public class TestDom4j { /** * dom4j生成XML步骤: 1.创建Document对象,Document document = * DocumentHelper.createDocument(); 2.生成根节点,Element root = * document.addElement("beans"); 3.添加标签,Element bean = root.addElement("bean"); * 4.给标签添加属性 5.依次重复3,4步骤可得到较为复杂的XML 6设置xml的输出格式,即对xml格式化 * * * dom4j解析XML步骤: 1.读取资源文件,获得Document对象,Document document = new * SAXReader().read(new File(path)); 2.获取根节点,Element root = * document.getRootElement(); * 3.根据特定标签(如根节点)称获取所有的指定标签(如bean), List<Element> list = root.elements("bean"); * 4.遍历所有指定标签的属性,这里和生成xml时略有不同,因为同一标签可能会出现多次, * for(Element bean : list) * {String id = bean.attributeValue("id");} *5.如子标签下还有子标签,重复3,4的步骤可得到所需的结果 * */ public static void main(String[] args) throws Exception { // System.out.println(generation("D:/user.xml")); try { // 获取解析完后的解析信息 HashMap<String, String> hashMap; // 遍历整个XML文件 hashMap = new HashMap<String, String>(); iterateWholeXML("D:/user.xml", hashMap); System.out.println("Id\t姓名\t密码"); for (int i = 0; i < hashMap.size(); i+=3) { int j = i/3; System.out.print(hashMap.get("user-id" + j) + "\t"); System.out.print(hashMap.get("name" + j) + "\t"); System.out.println(hashMap.get("password" + j) + "\t"); } } catch (Exception ex) { ex.printStackTrace(); } } /** * 采用dom4j生成xml * * @param path * : 生成文件的绝对路径,该参数仅在写入文件时有效 * @return */ public static String generation(String path) { // 声明xml字符串 String fileString = ""; // 创建Document对象 Document document = DocumentHelper.createDocument(); // 生成根节点(beans) Element root = document.addElement("users"); //在"users"根节点下添加了3个"user"标签 for (int i = 1; i < 3; i++) { // 第一层,在根节点下添加"bean"标签 Element bean = root.addElement("user"); // 给"bean"标签添加属性 bean.addAttribute("id", "" + i); bean.addElement("name").addText("name"+i); bean.addElement("password").addText("passoword"+i); } // 设置xml输出格式 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); java.io.Writer writer = new java.io.StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer, format); try { xmlWriter.write(document); fileString = writer.toString(); xmlWriter.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } // System.out.println(fileString); writeLocalFile(path, fileString);// 这里是写入本地文件。也可以将该字符串以流的形式输出到其他目的地 return fileString; } /** * 采用字符流将context写到指定文件 * @param path : 写入文件的绝对路径 * @param context : 写入的内容 */ public static void writeLocalFile(String path, String context) { try { java.io.Writer writer = new java.io.FileWriter(path); writer.write(context); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void iterateWholeXML(String filename,HashMap<String, String> hm) { SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(new File(filename)); Element root = document.getRootElement(); // 用于记录user编号的变量 int num = -1; // 遍历根结点(users)的所有孩子节点(肯定是user节点) for (Iterator iter = root.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); num++; // 获取user节点的id属性的值 Attribute idAttr = element.attribute("id"); if (idAttr != null) { String id = idAttr.getValue(); if (id != null && !id.equals("")) { hm.put(element.getName() + "-" + idAttr.getName() + num,id); } } // 遍历user结点的所有孩子节点(即name,password),并进行处理 for (Iterator iterInner=element.elementIterator(); iterInner.hasNext();) { Element elementInner = (Element) iterInner.next(); hm.put(elementInner.getName() + num,elementInner.getText()); } } } catch (DocumentException e) { e.printStackTrace(); } }