package test;import java.io.*;import org.dom4j.*;import org.dom4j.io.*;public class xmltest { public void writexml(){ document doc = org.dom4j.documenthelper.createdocument(); //创建节点操作对象 element rootelement = doc.addelement("student"); //创建根节点对象rootelement,标签名为student rootelement.settext("hello"); //对节点设置内容,后面读取xml文件的时候可以把他取出来 rootelement.addattribute("id","030511111"); //设置节点属性 element name = rootelement.addelement("name"); //添加子节点 name.settext("xiaozhang"); //设置子节点内容 element age = rootelement.addelement("age"); age.settext("23"); try { outputformat fmt = new outputformat(); //创建输出格式对象 fmt.setencoding("gb2312"); xmlwriter writer = new xmlwriter(fmt); //以输出格式为参数,创建xml文件输出对象 outputstream out = new fileoutputstream("d:\\test.xml"); //创建输出流.. writer.setoutputstream(out); //设置输出流 writer.write(doc); //输出doc对象,即形成xml文件 } catch (exception e) { e.printstacktrace(); } } public void readxml(){ saxreader reader = new saxreader(); //创建读取对象 document doc; try { doc = reader.read(new file("d:\\test.xml")); //从test.xml文件中取得节点操作对象 element name = doc.getrootelement(); //取得节点对象 system.out.println(name.gettext()); //输出节点内容 } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args){ xmltest s = new xmltest(); s.writexml(); s.readxml(); }}?