要使用dom4j框架首先下载dom4j.jar 包

一个例子:

package ytu.botao.xml.dom;
import java.io.FileOutputStream;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
 * 使用dom4j框架进行xml的生成文档和解析
 * @author botao
 *
 */
public class Dom4jTest {
    public static void main(String[] args) throws Exception
    {
        // 创建文档并设置文档的根元素节点 :第一种方式
        // Document document = DocumentHelper.createDocument();
        //
        // Element root = DocumentHelper.createElement("student");
        //
        // document.setRootElement(root);
        // 创建文档并设置文档的根元素节点 :第二种方式
        Element root = DocumentHelper.createElement("student");
        Document document = DocumentHelper.createDocument(root);
        root.addAttribute("name", "zhangsan");
        Element helloElement = root.addElement("hello");
        Element worldElement = root.addElement("world");
        helloElement.setText("hello");
        worldElement.setText("world");
        helloElement.addAttribute("age", "20");
        XMLWriter xmlWriter = new XMLWriter();
        xmlWriter.write(document);
          
        OutputFormat format = new OutputFormat("    ", true);
          
        XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"), format);
        xmlWriter2.write(document);
          
        XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"), format);
          
        xmlWriter3.write(document);
        xmlWriter3.close();
    }
}