http://www.cnblogs.com/zfc2201/archive/2011/08/16/2141441.html
http://www.blogjava.net/i369/articles/154264.html
package com.sfpay.test2; import java.io.File; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class ReadTest { public static void main(String[] args) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File("student2.xml")); Element root = document.getRootElement(); for(Iterator iter = root.elementIterator();iter.hasNext();){ Element e = (Element) iter.next(); System.out.print(e.getName()+"="); System.out.println(e.getText()); } System.out.println(document.asXML()); } }
package com.sfpay.test2; import java.io.FileOutputStream; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class WriterTest { public static void main(String[] args) throws IOException { Document document = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("student"); document.setRootElement(root); root.addElement("name").setText("zhangsan"); root.addElement("age").setText("20"); root.addElement("class").setText("df"); OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML编码 */ format.setEncoding("GBK"); XMLWriter writer = new XMLWriter(new FileOutputStream("test.xml"),format); writer.write(document); writer.close(); } }