Dom4j 如何创建一个 xml 文件

请参考下面的代码,下面的代码将会创建一个 xml 文件。

protected Document createDocument() throws Exception {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("system");

        Properties properties = System.getProperties();
        for (Enumeration elements = properties.propertyNames(); elements
                .hasMoreElements();) {
            String name = (String) elements.nextElement();
            String value = properties.getProperty(name);
            Element element = root.addElement("property");
            element.addAttribute("name", name);
            element.addText(value);
        }
        return document;
    }

这个方法将会返回 Document 对象。

在创建 Document 对象的时候,先要进行初始化,然后向 Document 对象中添加 root 元素节点。

随后可以再向 root 中提交其他元素节点,比如说我们这里希望能够添加一个 property 的元素。

随后可以向 property 元素中添加属性,同时也可以添加值。

生成的 XML 文件如下。

<?xml version="1.0" encoding="UTF-8"?>

<system>
	<property name="java.runtime.name">Java(TM) SE Runtime Environment</property>
	<property name="sun.boot.library.path">C:\Program Files\Java\jre7\bin</property>
	<property name="java.vm.version">21.0-b17</property>
	<property name="java.vm.vendor">Oracle Corporation</property>
	<property name="java.vendor.url">http://java.oracle.com/</property>
	<property name="path.separator">;</property>
	<property name="java.vm.name">Java HotSpot(TM) 64-Bit Server VM</property>
	<property name="file.encoding.pkg">sun.io</property>
	<property name="user.script"></property>
	<property name="user.country">US</property>
	<property name="sun.java.launcher">SUN_STANDARD</property>
	<property name="sun.os.patch.level"></property>
	<property name="java.vm.specification.name">Java Virtual Machine Specification</property>
	<property name="user.dir">D:\workdir\Repositories\p\dom4j\trunk</property>
	<property name="java.runtime.version">1.7.0-b147</property>
	<property name="java.awt.graphicsenv">sun.awt.Win32GraphicsEnvironment</property>
	<property name="java.endorsed.dirs">C:\Program Files\Java\jre7\lib\endorsed</property>
	<property name="os.arch">amd64</property>
	<property name="java.io.tmpdir">C:\Users\Yucheng\AppData\Local\Temp\</property>
	<property name="line.separator"></property>
	<property name="java.vm.specification.vendor">Oracle Corporation</property>
	<property name="user.variant"></property>
	<property name="os.name">Windows NT (unknown)</property>
	<property name="sun.jnu.encoding">GBK</property>
	<property name="java.library.path">C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;c:\Program Files (x86)\Intel\iCLS Client\;c:\Program Files\Intel\iCLS Client\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Windows Live\Shared;D:\Dkits\ant/bin;D:\Dkits\Android\android-sdk\tools;;.</property>
	<property name="java.specification.name">Java Platform API Specification</property>
	<property name="java.class.version">51.0</property>
	<property name="sun.management.compiler">HotSpot 64-Bit Tiered Compilers</property>
	<property name="os.version">6.2</property>
	<property name="user.home">C:\Users\Yucheng</property>
	<property name="user.timezone"></property>
	<property name="java.awt.printerjob">sun.awt.windows.WPrinterJob</property>
	<property name="file.encoding">UTF-8</property>
	<property name="java.specification.version">1.7</property>
	<property name="user.name">Yucheng</property>
	<property name="java.class.path">D:\workdir\Repositories\p\dom4j\trunk\bin;D:\workdir\Repositories\p\dom4j\trunk\lib\endorsed\xml-apis-2.0.2.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\test\jsr173_1.0_ri.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\test\junit-3.8.1.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\test\junitperf-1.8.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\isorelax-20030108.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\jaxme-0.3.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\jaxme-js-0.3.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\jaxme-xs-0.3.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\jtidy-4aug2000r7-dev.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\xalan-2.5.1.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\tools\xercesImpl-2.6.2.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\jaxen-1.1-beta-6.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\jaxme-api-0.3.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\jsr173_1.0_api.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\msv-20030807.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\pull-parser-2.1.10.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\relaxngDatatype-20030807.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\xpp3-1.1.3.3.jar;D:\workdir\Repositories\p\dom4j\trunk\lib\xsdlib-20030807.jar</property>
	<property name="java.vm.specification.version">1.7</property>
	<property name="sun.arch.data.model">64</property>
	<property name="java.home">C:\Program Files\Java\jre7</property>
	<property name="sun.java.command">org.dom4j.samples.CreateXMLDemo</property>
	<property name="java.specification.vendor">Oracle Corporation</property>
	<property name="user.language">en</property>
	<property name="awt.toolkit">sun.awt.windows.WToolkit</property>
	<property name="java.vm.info">mixed mode</property>
	<property name="java.version">1.7.0</property>
	<property name="java.ext.dirs">C:\Program Files\Java\jre7\lib\ext;C:\Windows\Sun\Java\lib\ext</property>
	<property name="sun.boot.class.path">C:\Program Files\Java\jre7\lib\resources.jar;C:\Program Files\Java\jre7\lib\rt.jar;C:\Program Files\Java\jre7\lib\sunrsasign.jar;C:\Program Files\Java\jre7\lib\jsse.jar;C:\Program Files\Java\jre7\lib\jce.jar;C:\Program Files\Java\jre7\lib\charsets.jar;C:\Program Files\Java\jre7\classes</property>
	<property name="java.vendor">Oracle Corporation</property>
	<property name="file.separator">\</property>
	<property name="java.vendor.url.bug">http://bugreport.sun.com/bugreport/</property>
	<property name="sun.cpu.endian">little</property>
	<property name="sun.io.unicode.encoding">UnicodeLittle</property>
	<property name="sun.desktop">windows</property>
	<property name="sun.cpu.isalist">amd64</property>
</system>