| 发表于:2007-02-01 16:45:131楼 得分:0 |
public int createxmlfile(string filename){ /** 返回操作结果, 0表失败, 1表成功 */ int returnvalue = 0; /** 建立document对象 */ document document = documenthelper.createdocument(); /** 建立xml文档的根books */ element bookselement = document.addelement( "books "); /** 加入一行注释 */ bookselement.addcomment( "this is a test for dom4j, holen, 2004.9.11 "); /** 加入第一个book节点 */ element bookelement = bookselement.addelement( "book "); /** 加入show属性内容 */ bookelement.addattribute( "show ", "yes "); /** 加入title节点 */ element titleelement = bookelement.addelement( "title "); /** 为title设置内容 */ titleelement.settext( "dom4j tutorials "); /** 类似的完成后两个book */ bookelement = bookselement.addelement( "book "); bookelement.addattribute( "show ", "yes "); titleelement = bookelement.addelement( "title "); titleelement.settext( "lucene studing "); bookelement = bookselement.addelement( "book "); bookelement.addattribute( "show ", "no "); titleelement = bookelement.addelement( "title "); titleelement.settext( "lucene in action "); /** 加入owner节点 */ element ownerelement = bookselement.addelement( "owner "); ownerelement.settext( "o 'reilly "); try{ /** 将document中的内容写入文件中 */ xmlwriter writer = new xmlwriter(new filewriter(new file(filename))); writer.write(document); writer.close(); /** 执行成功,需返回1 */ returnvalue = 1; }catch(exception ex){ ex.printstacktrace(); } return returnvalue; } | | |
|