java使用Jdom实现xml文件写入操作实例
本文实例讲述了java使用Jdom实现xml文件写入操作的方法。分享给大家供大家参考,具体如下:
packagecom.yanek.demo.xml.test; importjava.io.File; importjava.io.FileWriter; importorg.jdom.Attribute; importorg.jdom.Document; importorg.jdom.Element; importorg.jdom.input.SAXBuilder; importorg.jdom.output.XMLOutputter; publicclassJdomWriteXml{ /** *@paramargs */ publicstaticvoidmain(String[]args){ SAXBuildersb=newSAXBuilder(); Elementactions=newElement("actions"); Documentdocument=newDocument(actions); Elementaction1=newElement("action"); actions.addContent(action1); Attributepath_atbt1=newAttribute("path","/test"); Attributeclass_atbt1=newAttribute("class", "com.mystruts.demo.LoginAction"); action1.setAttribute(path_atbt1); action1.setAttribute(class_atbt1); Elementaction1_forward1=newElement("forward"); action1.addContent(action1_forward1); Attributeaction1_forward1_name_atbt1=newAttribute("name","success"); Attributeaction1_forward1_url_atbt1=newAttribute("url","test.jsp"); action1_forward1.setAttribute(action1_forward1_name_atbt1); action1_forward1.setAttribute(action1_forward1_url_atbt1); Elementaction1_forward2=newElement("forward"); action1.addContent(action1_forward2); Attributeaction1_forward1_name_atbt2=newAttribute("name","failure"); Attributeaction1_forward1_url_atbt2=newAttribute("url", "failure.jsp"); action1_forward2.setAttribute(action1_forward1_name_atbt2); action1_forward2.setAttribute(action1_forward1_url_atbt2); Elementaction2=newElement("action"); actions.addContent(action2); Attributepath_atbt2=newAttribute("path","/user"); Attributeclass_atbt2=newAttribute("class", "com.mystruts.demo.UserAction"); action2.setAttribute(path_atbt2); action2.setAttribute(class_atbt2); Elementaction2_forward1=newElement("forward"); action2.addContent(action2_forward1); Attributeaction2_forward1_name_atbt1=newAttribute("name","success"); Attributeaction2_forward1_url_atbt1=newAttribute("url","test.jsp"); action2_forward1.setAttribute(action2_forward1_name_atbt1); action2_forward1.setAttribute(action2_forward1_url_atbt1); Elementaction2_forward2=newElement("forward"); action2.addContent(action2_forward2); Attributeaction2_forward1_name_atbt2=newAttribute("name","failure"); Attributeaction2_forward1_url_atbt2=newAttribute("url", "failure.jsp"); action2_forward2.setAttribute(action2_forward1_name_atbt2); action2_forward2.setAttribute(action2_forward1_url_atbt2); Attributeroot_atbt1=newAttribute("m","001"); actions.setAttribute(root_atbt1); try{ Filef1=newFile("mystruts.xml"); //XMLOutputterxo=newXMLOutputter("",true,"GB2312"); XMLOutputterxo=newXMLOutputter(); FileWriterfw=newFileWriter(f1); xo.output(document,fw); fw.close(); }catch(Exceptione){ e.printStackTrace(); } //System.out.println(document.toString()); } }
生成xml文件:
<?xmlversion="1.0"encoding="UTF-8"?> <actionsm="001"> <actionpath="/test"class="com.mystruts.demo.LoginAction"> <forwardname="success"url="test.jsp"/> <forwardname="failure"url="failure.jsp"/> </action> <actionpath="/user"class="com.mystruts.demo.UserAction"> <forwardname="success"url="test.jsp"/> <forwardname="failure"url="failure.jsp"/> </action> </actions>
希望本文所述对大家Java程序设计有所帮助。