Dom4j学习笔记 为了备忘以及和大家交流学习,写了这篇笔记,如果我的学习笔记中有错误和不足的地方,请大家批评指教,谢谢! 一 创建xml文件 package com.qq.model; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import java.io.IOException; import java.io.FileWriter; /** */ /** * @author qiu秋 * @param document: * @param path:xml文档生成路径 */ public class Dom4jCreateXml ... { /** *//**输出XML数据 * @param document * @param path 新xml文件生成路径 * @throws IOException */ public static void write(Document document,String path) throws IOException ...{ // xml 标准格式,保存到文件 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileWriter( path ), format ); writer.write( document ); writer.close(); // 紧凑的格式,System.out显示(console) format = OutputFormat.createCompactFormat(); writer = new XMLWriter( System.out, format ); System.out.println(" ------------------Start------------------"); writer.write( document ); System.out.println(" -------------------End-------------------"); writer.close(); }//~write() /** *//** * 生成文档 * @return Document */ public static Document createDocument()...{ Document document = DocumentHelper.createDocument(); return document; } /** *//** * 生成根元素 *@param document *@param root *@return Element */ public static Element createRootElement(Document document,String root)...{ Element roots = document.addElement(root); return roots; } /** *//**生成元素 * @param root * @param name * @return */ public static Element createChildElement(Element root,String name)...{ Element ChildElement = root.addElement(name); return ChildElement; } /** *//**生成元素属性 * @param childElement * @param id * @param property */ public static void createProperty(Element childElement,String id,String property)...{ childElement.addAttribute(id, property); } public static void main(String arg[])...{ Document document = createDocument(); Element root = createRootElement(document,"employee"); Element childOne = createChildElement(root,"qiuqiu"); Element childTwo = createChildElement(root,"jignjign"); createProperty(childOne,"name","on"); createProperty(childTwo,"name","off"); try ...{ write(document,"w:/employee.xml"); } catch (IOException e) ...{ System.out.println(e.getMessage()); } }//~main()} 二. 修改xml文件 package com.qq.model; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; /** */ /** * @author qiu秋 * Dom4j解析器:获取、修改XML元素 */ public class Dom4jModifyXML ... { /** *//** * 修改XML元素值 * @param document * @param attribute 属性名 * @param attrValue 属性值 * @param value 元素值 * @throws IOException */ public static void setValue(Document document,String attribute,String attrValue,String value) throws IOException...{ Element root = document.getRootElement(); //System.out.println(root.getName()); //打印出根元素beans Element foo = null; //注意:root中为根元素的子元素。 for (Iterator i = root.elementIterator("author"); i.hasNext();) ...{ foo = (Element) i.next(); if(foo.attributeValue(attribute).equals(attrValue))...{ foo.setText(value); //写入文件 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gbk"); XMLWriter writer = null; try ...{ writer = new XMLWriter(new FileWriter("w:/new.xml"),format); writer.write(document); } catch (IOException e1) ...{ // TODO 自动生成 catch 块 e1.printStackTrace(); }finally...{ writer.close(); } } }//~for() }//~getValue()// 获取元素 public static void getValue()...{ //略 } public static void main(String[] args) ...{ // TODO 自动生成方法存根 try...{ Document document = Dom4jParserXML.Parser("W:/Employee.xml"); Dom4jParserXML.Writer(document); setValue(document,"name","James","Jack Kang"); System.out.println(" ********************修改元素值后***************************"); Dom4jParserXML.Writer(document); }catch(Exception e)...{ e.printStackTrace(); } }//~main()} 三 解析xml package com.qq.model; import java.io.File; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** */ /** * @author qiu秋 * @param args * 获取XML文件中的数据 */ public class Dom4jParserXML ... { /** *//** * 根据xml文件路径得到Document实例。 * @param path * @return * @throws DocumentException */ public static Document Parser(String path)throws DocumentException...{ SAXReader reader = new SAXReader(); Document document = reader.read(new File(path)); return document; }//~Parser() /** *//** * @param document * */ public static void Writer(Document document) throws IOException...{ // xml的格式,System.out显示 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gbk"); XMLWriter writer = new XMLWriter(System.out,format); System.out.println(" ---------------------start---------------------"); writer.write(document); System.out.println(" ---------------------end-----------------------"); }//Writer() public static void main(String[] args) ...{ // TODO 自动生成方法存根 try ...{ Document document = Parser("W:/Employee.xml"); Writer(document); } catch (Exception e) ...{ // TODO 自动生成 catch 块 e.printStackTrace(); } }}