以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  为什么这个用dom4j创建xml文件失败了呢?[求助]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=24909)


--  作者:nanhey
--  发布时间:12/2/2005 11:01:00 AM

--  为什么这个用dom4j创建xml文件失败了呢?[求助]
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Foo {
 public static Document createDocument() {
  Document document = DocumentHelper.createDocument();
  Element root = document.addElement("root");

  Element author1 = root.addElement("author").addAttribute("name",
    "James").addAttribute("location", "UK").addText(
    "James Strachan");

  Element author2 = root.addElement("author").addAttribute("name", "Bob")
    .addAttribute("location", "US").addText("Bob McWhirter");
 
  return document;
 }

 public static void main(String arg[]) throws IOException {
  Document document = createDocument();
  //File file=new File("./xml/foo.xml");
  File file=new File("d:\\foo.xml");
  FileWriter out = new FileWriter(file);
  document.write(out);
  System.out.println("asdf");
 }

}

---------------------------------------
程序能够运行,但是结果新产生的文件是空的,什么内容也没写进去,这是怎么回事?


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
27.344ms